endel / NativeWebSocket

🔌 WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)
Other
1.13k stars 155 forks source link

Network Disconnects - Abort or Close #56

Open lkarthee opened 2 years ago

lkarthee commented 2 years ago

I came across few issues with disconnect and reconnect scenarios. I am using C# version of Websocket:

Note: I have used Network Link Conditioner on Mac to simulate network conditions apart from testing on Windows and Android. Each one of them behaves little differently. This is because OS level network settings.

ClientWebSocket has three methods CloseAsync, CloseOutputAsyncand Abort.

Now coming back to NativeWebSocket, exposes websocket.Close() which calls CloseAsync() on ClientWebSocket. If network is disconnected, there is no point in calling CloseAsync or CloseOutputAsync as they are for terminating connections with acknowledgements. And there is no Abort method exists or a way to access ClientWebSocket as it is private.

For anyone who is curious as to how we detect network disconnects in our client - we use PingPong to send and receive msgs every n seconds/milliseconds. After sending a Ping, we don't receive a Pong within next n milliseconds - it is a disconnect.

I researched more about CloseAsync vs CloseOutputAsync and came across this article - https://mcguirev10.com/2019/08/17/how-to-close-websocket-correctly.html

Discussion points: