colyseus / colyseus-unity-sdk

⚔ Colyseus Multiplayer SDK for Unity
https://docs.colyseus.io/getting-started/unity-sdk/
MIT License
377 stars 104 forks source link

Exception during websocket.SendMessage result in broken state #232

Open Fafou opened 1 week ago

Fafou commented 1 week ago

Hi, First of all, I'm using colyseus 0.14.21 on android VR Headset device to create a VR game and I use the network ability of colyseus to create a viewer for spectator on windows device, so thank for this great library.

Some time sending message in a room result on exception, for exemple:

Unity   : One or more errors occurred. (The remote party closed the WebSocket connection without completing the close handshake.) 
Unity   :   at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions)
Unity   :   at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken)
Unity   :   at NativeWebSocket.WebSocket.SendMessage (System.Collections.Generic.List`1[T] queue, System.Net.WebSockets.WebSocketMessageType messageType, System.ArraySegment`1[T] buffer)

But sometime I had an other kind of exception, sorry I didn't kept a copy of the exception message, but it's something like You can not send message because socket it's in abnormal state: abort

After an exception in send message, the room stay broken, no more message will be sent.

I think this kind of exception come because of poor wifi, with "lot of" devices.

That is annoying, it's that the room didn't send message anymore.

After some debugging, I found a guilty in send message function If exception occured, the flag isSending is never reseted, I try to move :

// Note that we've finished sending.
lock (OutgoingMessageLock)
{
    isSending = false;
}

few line upper, in the finally block, and now, when exception occure, I get this exception 5 or 6 time because I try to send message on each frame, but after 100ms the socket is detected as broken and a reconnect occure. So, I my viewer I can have a freeze during less than 1 second but after the reconnection all object start moving.

I will try to make a Pull request in master, with this fix.

Fabien

Fafou commented 1 week ago

I think it can be related to this issue