Websocket Messages Throwing StackOverFlow Exception, WebSocketSharp, .NET 6

173 views Asked by At

I am working on a desktop application that connects to the League of Legends LCU API. I am using my own modified version of the PoniLCU Library to interact with the client. One issue I cannot seem to get around is the handling of Websocket events.

It works perfectly fine, except for when the program is running and the League Client is not, upon then starting the League Client. Not to long after a connection is made, a StackOverFlow exception is then thrown. This issue is also discussed on the PoniLCU Repo too. Here is my method:

        private void TryConnect()
        {
            try
            {
                if (IsConnected) return;

                var status = GetLeagueStatus();
                if (status == null) return;

                ProcessInfo = status;
                var byteArray = Encoding.ASCII.GetBytes("riot:" + status.Item2);
                Client!.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                SocketConnection = new WebSocket("wss://127.0.0.1:" + status.Item3 + "/", "wamp");
                SocketConnection.SetCredentials("riot", status.Item2, true);

                SocketConnection.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12;
                SocketConnection.SslConfiguration.ServerCertificateValidationCallback = (a, b, c, d) => true;

                // Add event handlers for OnOpen and OnClose
                SocketConnection.OnOpen += (sender, e) => OnWebSocketOpen();
                SocketConnection.OnClose += (sender, e) => OnWebSocketClose(e.Code, e.Reason, e.WasClean);
                SocketConnection.OnMessage += (sender, e) => onWebSocketMessage(sender!, e);

                SocketConnection.Connect();
                SocketConnection.Send($"[5, \"OnJsonApiEvent\"]");

                ProcessInfo = status;
                IsConnected = true;
            }
            catch (Exception e)
            {
                ProcessInfo = null;
                IsConnected = false;
                if (!IsConnected) throw new InvalidOperationException($"Exception occurred trying to connect to League of Legends: {e.ToString()}");
            }
        }

And by removing this line:

SocketConnection.Send($"[5, \"OnJsonApiEvent\"]");

The issue no longer occurs.

When the client is running, the events work fine and I can view them in my HandleMessage method being written to the output through Debug.Writeline.

I have tried inside of the HandleMessage method checking for duplicate events, but I realise now this likely doesn't mean anything as it's probably an issue before this stage. Or just with the sent events in general.

I have tried adding a Thread.Sleep delay in the hope of slowing down all of the requests. As on first launch the client produces a lot of requests in the output. As well as the connection to the client occurs twice. I have a message box that appears OnOpen, and this message box is displayed twice, along with the OnDisconnect one too. So I thought simply delaying the program might help with this but to no avail.

Another reason for my above thought was that when I launch the client with my program open, if I dont click on the message boxes for some time. I avoid the StackOverFlow exception, but then I also lose any events being displayed in the output from the handle message method. To note without my MessageBox the issue still pertains. The original Library has no such thing and still encounters the same issue.

This isn't my strong suite, I initially simply removed anything to do with the events. But I actually now would like to handle them so I know what the client is doing in the moment to display my programs options accordingly. I am not to sure where to go from here except using a different Library to WebSocketSharp but I could not implement anything that worked for me.

Further Note on the Repo it is mentioned about it being a .NET 6.0 issue. I am not sure if it is worth me downgrading or upgrading to avoid it like so. I did try downgrade but could not get it working after.

.NET 6, WebSocketSharp Library

0

There are 0 answers