doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
729 stars 125 forks source link

detecting disconnects #232

Open Jakobi-mirsk opened 2 years ago

Jakobi-mirsk commented 2 years ago

We are seeing that sometimes we do not get notified of disconnects. We are using the default values for reconnection. The server side (made in java-script) is getting notified when the client is not connected anymore. Is there anything we should keep in mind regarding the reconnection mechanism? It is not all the time we see this problem. Sometimes it works and sometimes it doesn't.

doghappy commented 2 years ago

These reasons will cause the client to no longer try to reconnect:

  1. The server has forcefully disconnected the socket with socket.disconnect()
  2. The client calls DisconnecdAsync()
  3. The reconnection counter is greater than Options.ReconnectionAttempts
  4. When Connecting, an uncaught exception is thrown

The default value of Options.Reconnection is true, in other cases, the client will keep trying to reconnect until the connection is successful or the above situations occur.

btw, are you using the latest version of the client library?

Jakobi-mirsk commented 2 years ago

hi I am using version 3.0.3 These are my options: options.Reconnection = true; options.ReconnectionAttempts = int.MaxValue; options.ReconnectionDelay = 1000; options.ReconnectionDelayMax = 5000; options.RandomizationFactor = 0.5;

If the server is disconnecting, would the OnDisconnected event fire? If and uncaught exception is thrown during connecting, would the OnError event fire?

doghappy commented 2 years ago

If the server is disconnecting, would the OnDisconnected event fire?

yeah

If and uncaught exception is thrown during connecting, would the OnError event fire?

no, it is fired upon a connection error. https://github.com/doghappy/socket.io-client-csharp/issues/231

Jakobi-mirsk commented 2 years ago

H, thank for the quick answers If the client is never sending but only receiving data from the server. how will it know, when the connection is down for some reason?

doghappy commented 2 years ago
socket.OnDisconnected+= Socket_OnDisconnected;

...

private void Socket_OnDisconnected(object sender, string e)
{
    Console.WriteLine("reason: " + e);
}
Jakobi-mirsk commented 2 years ago

Okay. I cannot rule out that there is something wrong with the server but we have to client that connect to the server on the same event and receives the same data. Only one of the clients did not receive any data for a long time. That is the reason I believe that there is something wrong with the connection.

Jakobi-mirsk commented 2 years ago

is there any way to enable some debug output in the library?

doghappy commented 2 years ago

You need to remove the reference from NuGet first, then clone this repo and add SocketIOClient.csproj to your project.

You can use Debug or other

https://github.com/doghappy/socket.io-client-csharp/blob/3149107ddd512a8fa68d3f15b80bdf4330910a37/src/SocketIOClient/Transport/TransportRouter.cs#L188

Of course, you can also use breakpoint debugging

oshere1111 commented 2 years ago

i have similiar issue after i got connected i turned off internet (on the client) and only after ~50 seconds i got disconnected even after doing this:

TimeSpan interval = new TimeSpan(0, 0, 10); //to get 10 seconds
SocketIOOptions options = new SocketIOOptions();
options.Reconnection = false;
options.ConnectionTimeout = interval;

and i didnt got any message on client.OnError and i tried it without putting value in ConnectionTimeout i tried that too:

socket.OnDisconnected+= Socket_OnDisconnected;

...

private void Socket_OnDisconnected(object sender, string e)
{
    Console.WriteLine("reason: " + e);
}

i got this: reason: transport close is there any way to get disconnected message faster (without adding client.DisconnectAsync();)?