Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket does not detect network switch and not calling OnClose event #516

Closed
emaadgh opened this issue Jan 2, 2019 · 4 comments
Closed

Comments

@emaadgh
Copy link

emaadgh commented Jan 2, 2019

Hi, I'm using Websocket-Sharp in Unity for a turn-based game.
There is a problem on detecting lost connection in some cases when for example in mobile devices user's Internet connection changes from 4G to 3G in the middle of the game, or in Windows, user turns on/off VPN in the middle of the game.
In this situations websocket-sharp thinks that is still connected and the connection readyState is open, but actually its not, and server messages won't recieve any more and websocket-sharp won't fire OnClose event.
I found a temporary solution to fix this issue by sending a command to server every second. When the issue I mentioned above happens, after some command sending, the websocket-sharp findout that the connection is lost and fires OnClose event.
Is there any direct control on Sending ping messages in websocket-sharp?
Is there any solution for this issue?

@emaadgh emaadgh closed this as completed Apr 7, 2019
@adhip94
Copy link

adhip94 commented Feb 25, 2021

What command did you send? Did you find an actual solution for this issue?

@ilsnk
Copy link

ilsnk commented Jun 29, 2022

maybe somebody know how to fix this?

@emaadgh
Copy link
Author

emaadgh commented Jul 4, 2022

Hi @ilsnk , I used the following workaround for this problem.

As you can see I used ws.Ping(); from WebsocketSharp and I called it in a Task.Run to prevent it from blocking the UI thread.

while (true)
{
    if (ws != null)
    {
        if (Application.isPlaying)
        {
            Task.Run(() => PingToServer());
        }
    }
    yield return new WaitForSeconds(1);
}
void PingToServer()
{
    bool ping = ws.Ping();
    if (!ping)
    {
        if (ws.ReadyState == WebSocketState.Open)
        {
            ws.CloseAsync();
        }
    }
}

Hope it help!

@ilsnk
Copy link

ilsnk commented Jul 9, 2022

this could help but i get a different error when i try to ping. it often happens when i have a lot of clients connected to my server and the server itself is connected to another service via websocket - and this connection breaks (but not detect lose connect) and i get this error

07/09/2022 10:54:24|Error|WebSocket.sendBytes:2015|Unable to write data to the transport connection: The socket has been shut down.
07/09/2022 10:54:24|Error|WebSocket.sendBytes:2016|System.IO.IOException: Unable to write data to the transport connection: The socket has been shut down. ---> System.Net.Sockets.SocketException: The socket has been shut down
                            at System.Net.Sockets.Socket.Send (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags) [0x00016] in <0463b2ef957545c0a51b42f372cd4fbb>:0 
                            at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x0009b] in <0463b2ef957545c0a51b42f372cd4fbb>:0 
                             --- End of inner exception stack trace ---
                            at System.Net.Sockets.NetworkStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x000e2] in <0463b2ef957545c0a51b42f372cd4fbb>:0 
                            at WebSocketSharp.WebSocket.sendBytes (System.Byte[] bytes) [0x00002] in from\WebSocketSharp\WSS\WebSocket.cs:2012 

I think i was able to fix it, not sure if this is correct but it worked for me.
I just catch IOException and close connect, and I think this solved my problem

WebSocket.cs
 private bool sendBytes (byte[] bytes)
 {
      try {
        _stream.Write (bytes, 0, bytes.Length);
      }
      catch (IOException e)
      { 
        _logger.Error (e.Message);
        _logger.Error (e.ToString ());
        close((ushort)CloseStatusCode.InvalidData, e.Message);

        return false;
      }
      catch (Exception ex) {
        _logger.Error (ex.Message);
        _logger.Error (ex.ToString ());

        return false;
      }

      return true;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants