websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

A series of reconnecting has failed

Open llee2015 opened this issue 6 years ago • 2 comments

I'm testing reconnection of the websocket client. After receiving OnClose event, my program calls the following code to reconnect (not in the OnClose thread): websocket.Close(); websocket.Connect();

After about 10 failure and retries, I got exception "A series of reconnecting has failed". The websocket-sharp code does have the logic of _maxRetryCountForConnect=10. Anyone knows why this limitation is needed at all? And what is the best practice to reconnect except create a new WebSocket object?

llee2015 avatar Nov 08 '19 07:11 llee2015

I use below solutions in my application: use a timer to periodically check what is the last time to receive the message, if it is longer than a threshold, then call Close() and Connect(). However, if you don’t change the websocketsharp source code, you will easily reach the max retry count.

The discussion here indicate that the questioner evetually use dispose and create a new websocke instance. https://github.com/sta/websocket-sharp/issues/122

eynzhang avatar Jan 21 '20 03:01 eynzhang

I have used this hack to get around the limitation temporarily but setting the maxCount too high will result in stack overflow

using System.Reflection;
[...]
var webSocketType = ws.GetType();
var maxRetryField = webSocketType.GetField("_maxRetryCountForConnect", BindingFlags.NonPublic | BindingFlags.Static);
if (maxRetryField != null)
    maxRetryField.SetValue(ws, 100);

thebitbrine avatar Jan 28 '24 17:01 thebitbrine