colyseus / colyseus-unity-sdk

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

websocket stabilize - format code #88

Closed Xii55 closed 5 years ago

Xii55 commented 5 years ago

The PR Goal is to stabilize the the rooms underlying WebSocket connection.

In our project my task was to improve the re-connection ability of the connection. We plan to deploy the project for mobile devices.

There are some cases, when the server closes connection, however the client still thinks the connection is at open state, or took a long time to realize a problem has occurred. Through out of my experience with the framework, I've encounters the following problems.

Most of the time the problem was, the Room didn't get notified about the WebSocket connectionState change, so the OnLeave event never fired.

For the CancelToken

The second part of the PR is about the code formatting. You can pull this as.

This changes tested with

endel commented 5 years ago

Nice, thanks for this @Xii55! If you don't mind sharing how you've handled the detection of the locked/unlocked state on iOS would be awesome, I'm sure other people would appreciate!

Cheers!

Xian55 commented 5 years ago

At this point using Unity's https://docs.unity3d.com/ScriptReference/NetworkReachability.html class to detect adapter changes. It works on all platforms(Windows, IOS, Android).

When the Application.internetReachability == NetworkReachability.NotReachable then i cut all of the connections under the Room manually client side, so when the adapter comes back, could open up a new websocket connection for the Room. That was the only solution to get rid of the Closed WebsocketState.

I've tried to use the native https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkavailabilityeventargs?view=netframework-4.8 but this is not well implemented in Mono. For example, on windows platform, tries to call a linux based function. on IOS cause deadlock.