Closed jingfu-wei closed 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 :
Make sure you have one socket.io instance, do something like this (or be creative about it): May this link be helpful too: https://github.com/doghappy/socket.io-client-csharp/issues/151
// inside SocketManager class
public static SocketManager Instance { get; private set; } = null;
public SocketIOUnity socket;
private void Start()
{
InitInstance();
}
public void InitInstance(bool forceInit = false)
{
if (forceInit || Instance == null)
{
Debug.Log("InitInstance");
Instance = this;
socket = new SocketIOUnity("https://www.example.com");
socket.OnUnityThread("spin", (res) =>
{
// TODO
});
socket.Connect();
DontDestroyOnLoad(transform.root.gameObject);
}
}
Pause socket receiving activity
public handleSocketReceiving = true;
void OnApplicationFocus(bool paused)
{
handleSocketReceiving = !paused;
}
void OnApplicationPause(bool paused)
{
handleSocketReceiving = !paused;
}
void OnApplicationQuit()
{
handleSocketReceiving = false;
}
// Somewhere
socket.OnUnityThread("spin", (res) =>
{
if(handleSocketReceiving){
//TODO
}
});
I hope these solve the problem.
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 :)
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 call
SocketIOUnity.Disconnect()
inOnApplicationQuit()
andOnDestroy()
to see if I could destroy the child thread, and it failed to do so. I thereby setSocketIOUnity.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 :)