4

Can anyone explain me how to remove websockets from clients that terminates their connection unexpectedly? I mean that the user closes the application in a bad way, without telling the server that they want to disconnect? And how generally you handle disconnection.

I tried one piece of code that listen for message receive and has a cancellationTokenSource with 3s, so it will check if the connection is still alive. When I close the websocket client application the server do not go to the WebSocketState.Closed part, and when the cancellation token is triggered, the server drops my connection to client. I am doing something wrong here ?! Thanks in advance!

while (socket.State == WebSocketState.Open)
{
   var cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(3000));
   buffer = new ArraySegment<byte>(new byte[4096]);

   WebSocketReceiveResult received = null;


   try
   {
       received = await socket.ReceiveAsync(buffer, cancellationSource);
   }
   catch (AggregateException e)
   {
       continue;
   }

   switch (received.MessageType)
   {
       case WebSocketMessageType.Close:

           HandleClose(socket);

       break;

       case WebSocketMessageType.Text:
           // Handle text message 

       break;
   }
}

if (socket.State == WebSocketState.Aborted)
{
    // Handle aborted
}
else if (socket.State == WebSocketState.Closed)
{
    HandleClose(socket);
}
else if (socket.State == WebSocketState.CloseReceived)
{

}
else if (socket.State == WebSocketState.CloseSent)
{
}
5
  • If client closes TCP connection unexpectedly, the TCP stack will eventually notify the application layer, but not before the proper protocols are completed. Those protocols vary depending on how the user ended the connection. Remember TCP tries to keep connections up and will retry up to 10 times varying the retry times. If the client closes in a way that the client TCP side sends a RST or FIN, then the stack will start, in priority order, to clean up. When done, the application is notified. Commented Jun 1, 2017 at 16:54
  • @JohnPeters, thanks for the answer! By your opinion, is it suitable for real time game server or just to stick with the old and fine way, udp sockets ? Commented Jun 2, 2017 at 6:02
  • Do you found proper solution? I have the same problem Commented May 8, 2018 at 7:22
  • @Gabrielkotev did you find any solution for this? Commented Oct 10, 2018 at 14:24
  • Nope. Unfortunately I left it for now :( Commented Oct 17, 2018 at 9:09

1 Answer 1

7

In case the client closes the connection unexpectedly, ReceiveAsync will throw a WebSocketException.

try
{
    var received = await webSocketConnection.ReceiveAsync(...);
}
catch(WebSocketException webSocketException)
{
    if(webSocketException.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
    {
        //custom logic
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

pay attention on SO_KEEPALIVE and SO_CLOSE, in the worst case the connection drop without sending any CLOSE, and the client established 7200 seconds in SO_KEEPALIVE , possible workaround --> PING at WS application level is expected otherwise server begin a force close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.