itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
393 stars 67 forks source link

Auto reconnection keeps running after exit play mode in Unity editor #5

Closed jingfu-wei closed 2 years ago

jingfu-wei commented 2 years ago

Greetings! I am having so much fun with SocketIOUnity. Thank you very much for this.

I have run into a problem, where I found the child thread keeps running in the background even though the main thread is exited. I tried to manually callSocketIOUnity.Disconnect() in OnApplicationQuit() and OnDestroy() to see if I could destroy the child thread, and it failed to do so. I thereby set SocketIOUnity.Options.Reconnection = false to stop auto-reconnection to the server and it worked (i.e. not attempting to reconnect to the server).

However, I am not sure if this can kill all background threads. Am I doing anything wrong? Is there any proper way to exit the main thread with all background threads killed?

Thanks again :)

itisnajim commented 2 years ago

I'm really glad you find the SocketIOUnity repository useful.

So for the Editor, I mentioned that i didn't tested, and I noticed that the behavior of the editor varies by unity version number, so don't count on it, check on pc or mobile devices. But these some of my suggestions / recommendation :

I hope these solve the problem.

jingfu-wei commented 2 years ago

Thank you very much for the advice. I will import your code snippets into my code.

So background threads can quit properly in a complied executable, right? Is it necessary to manually abort background threads with the code?

Thanks again :)

itisnajim commented 2 years ago

not necessary, if the app quite/exit, the background threads will also be terminated, unless you created and run the package on android or ios background service.

welcome :)