sta / sta/websocket-sharp

How to catch exceptions that occur in websocket sharp

Open
#639 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

Good day I keep hitting an exception after a few hours of use;

22/11/2020 13:03:07|Fatal|WebSocket.<startReceiving>b__176_2|WebSocketSharp.WebSocketException: The header of a frame cannot be read from the stream.
                             at WebSocketSharp.WebSocketFrame.processHeader(Byte[] header)
                             at WebSocketSharp.WebSocketFrame.<>c__DisplayClass73_0.<readHeaderAsync>b__0(Byte[] bytes)
                             at WebSocketSharp.Ext.<>c__DisplayClass48_0.<ReadBytesAsync>b__0(IAsyncResult ar)
Websocket closed connection

I tried wrapping my entire websocket initialisation and OnBinds in a try {} catch {} but the exception still halts the entire program, what is the correct watch to catch this ... or alternatively catch everything from websocketsharp, basically on failure/error the thread is restarted by main; but it never gets chance as the entire program exits

Example code block (with trys that do not work):

        private void gdaxWebSocketFeed(object passedArgs)
        {
            object[] passedArgsArray = (object[])passedArgs;
​
            ulong packetID = 0;
            int websocketID = (int)passedArgsArray[0];
            Dictionary<int,ConcurrentQueue<string>> sendQueue = 
                (Dictionary<int,ConcurrentQueue<string>>)passedArgsArray[1];
            string wsHost = (string)passedArgsArray[2];
​
            try
            {
                WebSocket wsClient = new WebSocket(wsHost);
                //wsClient.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
​
                wsClient.OnOpen += (sender, e) =>
                {
                    parentFunction("OPEN", packetID++, new object[] { websocketID, e });
                };
                wsClient.OnError += (sender, e) =>
                {
                    parentFunction("ERROR", packetID++, new object[] { websocketID, "error", e });
                };
                wsClient.OnClose += (sender, e) =>
                {
                    parentFunction("CLOSE", packetID++, new object[] { websocketID, e });
                };
                wsClient.OnMessage += (sender, e) =>
                {
                    GDAXExchangePacket CastJSON =
                        JsonConvert.DeserializeObject<GDAXExchangePacket>(e.Data);
                    parentFunction("MESSAGE", packetID++, new object[] { websocketID, CastJSON, e.Data });
                };
​
                wsClient.Connect();
​
                while (wsClient.IsAlive)
                {
                    if (sendQueue.Count == 0)
                    {
                        Thread.Sleep(100);
                    }
                    else if (sendQueue[websocketID].TryDequeue(out string sendData))
                    {
                        if (sendData != null)
                        {
                            wsClient.Send(sendData);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                parentFunction("ERROR", packetID++, new object[] { websocketID, "exception", exception.Message });
            }
        }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the WebSocket. stack entry and WebSocketFrame.processHeader shown in the report, then compare that path with the gdaxWebSocketFeed try/catch and the OnError callback. Done means the exception is handled without terminating the program, allowing the caller to restart the websocket thread as requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.