itisnajim / SocketIOUnity

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

Double connection #16

Closed tho1294 closed 2 years ago

tho1294 commented 2 years ago

Hello i have a problem when i use your plugin i have a double connection on my server do you have any idea to fix that?

itisnajim commented 2 years ago

This is one of the ways, you can put the SocketManager into the Camera object (in one scene, the home/first one for the example, or the one after login, don't do it (putting the SocketManager/Script to all cameras of all scenes), then use DontDestroyOnLoad at the initiation

    void Start() // or Awake()
    {
        if(socket == null){ // or if(!socket)
            StartSocketConnection();
            DontDestroyOnLoad(transform.root.gameObject);
        }
    }
    void StartSocketConnection()
    {
        var uri = new Uri("https://example.com");
        socket = new SocketIOUnity( ...
        ...
    }
tho1294 commented 2 years ago

I put only one script in my scene, not on camera i try with the DontDestroyOnLoad too but i have the same issue

i can send you my code if you want

itisnajim commented 2 years ago

Somehow you called Connect or ConnectAsync multiple times

socket = new SocketIOUnity...;
// line bellow should be called one time in the runtime or one time before Disconnecting it
socket.Connect();
tho1294 commented 2 years ago
    private void connectToSignallingServer()
    {
        var uri = new Uri("http://192.168.0.15:3000");
        socket = new SocketIOUnity(uri, new SocketIOOptions
        {
            EIO = 4,
            Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
        });

        socket.On("connect", (response) =>
        {
            Debug.Log("connectToSignallingServer: connect");
            socket.Emit("create or join", "foo");
        });
        socket.On("ipaddr", (response) => { Debug.Log("connectToSignallingServer: ipaddr"); });
        socket.On("created", (response) =>
        {
            Debug.Log("connectToSignallingServer: created");
            isInitiator = true;
        });
        socket.On("full", (response) => { Debug.Log("connectToSignallingServer: full"); });
        socket.On("join", (response) =>
        {
            Debug.Log("connectToSignallingServer: join");
            Debug.Log("connectToSignallingServer: Another peer made a request to join room");
            Debug.Log("connectToSignallingServer: This peer is the initiator of room");
            isChannelReady = true;
        });
        socket.On("joined", (response) =>
        {
            Debug.Log("connectToSignallingServer: joined");
            isChannelReady = true;
        });
        socket.On("log", (response) =>
        {
            //TODO
            /*for (Object arg : response) {
                Debug.Log(  "connectToSignallingServer: " + String.valueOf(arg));
            }*/
        });
        socket.On("message", (response) => { Debug.Log("connectToSignallingServer: got a message"); });
        socket.On("message", (response) => {Debug.Log("connectToSignallingServer: got a message");  });
        socket.On("disconnect", (response) => { Debug.Log("connectToSignallingServer: disconnect"); });

        socket.Connect();
    }
itisnajim commented 2 years ago

This is one of the ways, you can put the SocketManager into the Camera object (in one scene, the home/first one for the example, or the one after login, don't do it (putting the SocketManager/Script to all cameras of all scenes), then use DontDestroyOnLoad at the initiation

    void Start() // or Awake()
    {
        if(socket == null){ // or if(!socket)
            StartSocketConnection();
            DontDestroyOnLoad(transform.root.gameObject);
        }
    }
    void StartSocketConnection()
    {
        var uri = new Uri("https://example.com");
        socket = new SocketIOUnity( ...
        ...
    }

this should do the job, if you are testing in the Editor.. this is a Unity Editor issue, pause or stop the viewport (preview) doesn't stop the socket connection until you kill the editor ... test directly into a pc/mac app.

tho1294 commented 2 years ago

i try all of that i try your sample i build / restart my computer but i don't understand why i continue to investigate and i tell you if i found a solution

itisnajim commented 2 years ago

i forgot to mention that the socket object should be static

public static SocketIOUnity socket = null;

so the code should be like this

public class SocketManager : MonoBehaviour
{
    ...

    public static SocketIOUnity socket = null;

    void Start() // or Awake()
    {
        if(SocketManager.socket == null){ // or if(!SocketManager.socket)
            StartSocketConnection();
            DontDestroyOnLoad(transform.root.gameObject);
        }
    }
    void StartSocketConnection()
    {
        var uri = new Uri("https://example.com");
        SocketManager.socket = new SocketIOUnity( ...
        ...
    }

    ...

Multiple connections = you called socket.Connect() StartSocketConnection() or connectToSignallingServer() method multiple times, and avoid testing your game app using the Editor.

tho1294 commented 2 years ago

yes i put log in StartSocketConnection and it's call one time i try with build on android and on windows

itisnajim commented 2 years ago

I'll close this issue as I feel it has been resolved. Feel free to comment again if this is not the case and we can reopen the issue.