floatinghotpot / socket.io-unity

socket.io client for Unity, power game client with node.js back-end
494 stars 76 forks source link

Reconnection not working ... #38

Open sfranzyshen opened 5 years ago

sfranzyshen commented 5 years ago

the reconnection is not working. I get one attempt and then nothing ... here's an example from the code below ... I'm using the latest lib/'s and code from github with unity 2017.3.1f1 I tried all kinds of settings for the reconnect options ... but nothing!

INPUT/PROCEDURE             OUTPUT/RESULT           NOTES:
-------------------         -------------------------   -----------
Start Unity Program     =>  Socket Connected        everything works as expected
(Server running)
Kill the Server         =>  Socket Disconnected     client disconnects as expected
                    Socket Reconnect Attempt    no additional attempts are made
                    Socket Reconnecting     no timeout happends

Restart the Server      =>                  no reconnection happends
                                    and no additional attempts made
Stop Unity Program      =>
Start Unity Program     =>                  no 'socket connected' output but
                                    a connection is made with sever

                    Socket Connect Error        this output seems to be older
                    Socket Connect Error        messages, and not what is 
                    Socket Reconnect Error      currently happening ...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Quobject.SocketIoClientDotNet.Client;
using Newtonsoft.Json;

public class ChatDataEdit {
    public string value;
};

public class SocketIOScriptEdit : MonoBehaviour {
    public string serverURL = "http://node0.local:8080";

    public SU_SpaceshipSocket SpaceShipScript;

    protected Socket socket = null;
    protected IO.Options options = null;
    protected List<string> chatLog = new List<string> ();

    private IO.Options CreateOptions()
    {
        IO.Options op = new IO.Options();
        op.AutoConnect = true;
        op.Reconnection = true;
        op.ForceNew = true;
        op.Timeout = -1;
        op.ReconnectionAttempts = 100;
        return op;
}

    private int userId = 99;

    void Destroy() {
        DoClose ();
    }

    void Start () {
        DoOpen ();
    }

    // Update is called once per frame
    void Update () {
    }

    void DoOpen() {
        if (socket == null) {
            options = CreateOptions();
            socket = IO.Socket (serverURL, options);

            socket.On(Socket.EVENT_CONNECT_TIMEOUT, () => {
                Debug.Log("Socket Connect Timeout");
            });

            socket.On(Socket.EVENT_CONNECT_ERROR, () => {
                Debug.Log("Socket Connect Error");
            });

            socket.On(Socket.EVENT_ERROR, () => {
                Debug.Log("Socket Error");
            });

            socket.On(Socket.EVENT_RECONNECT, () => {
                Debug.Log("Socket Reconnect");
            });

            socket.On(Socket.EVENT_RECONNECT_ATTEMPT, () => {
                Debug.Log("Socket Reconnect Attempt");
            });

            socket.On(Socket.EVENT_RECONNECT_FAILED, () => {
                Debug.Log("Socket Reconnect Failed");
            });
            socket.On(Socket.EVENT_RECONNECT_ERROR, () => {
                Debug.Log("Socket Reconnect Error");
            });
            socket.On(Socket.EVENT_RECONNECTING, () => {
                Debug.Log("Socket Reconnecting");
            });

            socket.On(Socket.EVENT_CONNECT_ERROR, () => {
                Debug.Log("Socket Connect Error");
            });

            socket.On (Socket.EVENT_CONNECT, () => {
                SendConnected();
                Debug.Log("Socket Connected");
            });

            socket.On(Socket.EVENT_DISCONNECT, () => {
                Debug.Log("Socket Disconnected");
            });

            socket.On ("publish", (data) => {
                Debug.Log(data);
                ChatDataEdit chat = JsonConvert.DeserializeObject<ChatDataEdit> (data.ToString());
                SpaceShipScript.setActions(chat.value);
            });
        }
    }

    void DoClose() {
        if (socket != null) {
            socket.Disconnect ();
            socket = null;
            Debug.Log("Closed Socket");
        }
    }

    void SendChat(string str) {
        if (socket != null) {
            string msg = "{ value: '" + userId + ", " + str + "' }";
            socket.Emit ("publish", msg);
        }
    }

    void SendConnected()
    {
        if (socket != null)
        {
            socket.Emit("connected", userId);
        }
    }
}
sfranzyshen commented 5 years ago

I found another project that is working with unity 2017.4.14f1 + socket.io 2.1.1 and the reconnection is working !