Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jingfu-wei opened this issue Feb 24, 2022 · 3 comments
Closed

Comments

@jingfu-wei
Copy link

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
Copy link
Owner

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 :

// 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.

@jingfu-wei
Copy link
Author

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
Copy link
Owner

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants