public static SocketIOMessage FromString(string msg)
    {
        var m = new SocketIOMessage ();
            var t = 0;
            if (int.TryParse (NextPart (msg, out msg), out t)) {
                m.type = (FrameType)t;
            }
            var id = NextPart (msg, out msg);
            if (id == null) {
                m.id = null;
                m.isUser = false;
            } else {
                if (id.EndsWith ("+")) {
                    m.isUser = true;
                    id = id.Substring (0, id.Length - 1);
                }
                int i;
                if (int.TryParse (id, out i)) {
                    m.id = i;
                }
            }
            m.endPoint = NextPart (msg, out msg);
            if(msg.Length > 0)
                m.data = msg.Substring(1);

            return m;
    }
Example #2
0
    /// <summary>
    /// Send the specified payload as a JSON message to the server.
    /// </summary>
    /// <param name='payload'>
    /// Payload.
    /// </param>
    public int Send(object payload)
    {
        var m = new SocketIOMessage();

        m.type = SocketIOMessage.FrameType.JSONMESSAGE;
        m.data = HTTP.JsonSerializer.Encode(payload);
        return(Send(m));
    }
 /// <summary>
 /// Sends an event to the server.
 /// </summary>
 /// <param name='eventName'>
 /// Event name.
 /// </param>
 /// <param name='args'>
 /// Arguments.
 /// </param>
 public int Emit(string eventName, params object[] args)
 {
     var m = new SocketIOMessage();
     m.type = SocketIOMessage.FrameType.EVENT;
     var payload = new Hashtable();
     payload["name"] = eventName;
     payload["args"] = args;
     m.data = HTTP.JsonSerializer.Encode(payload);
     return Send (m);
 }
 /// <summary>
 /// Send a raw SocketIOMessage to the server.
 /// </summary>
 /// <param name='msg'>
 /// Message.
 /// </param>
 public int Send(SocketIOMessage msg)
 {
     msg.id = msgUid++;
     if(socket == null) {
         Debug.LogError("Socket.IO is not initialised yet!");
         return -1;
     } else {
         socket.Send (msg.ToString ());
         return msg.id.Value;
     }
 }
Example #5
0
    /// <summary>
    /// Sends an event to the server.
    /// </summary>
    /// <param name='eventName'>
    /// Event name.
    /// </param>
    /// <param name='args'>
    /// Arguments.
    /// </param>
    public int Emit(string eventName, params object[] args)
    {
        var m = new SocketIOMessage();

        m.type = SocketIOMessage.FrameType.EVENT;
        var payload = new Hashtable();

        payload["name"] = eventName;
        payload["args"] = args;
        m.data          = HTTP.JsonSerializer.Encode(payload);
        return(Send(m));
    }
Example #6
0
    IEnumerator Heartbeat()
    {
        var beat = new SocketIOMessage();

        beat.type = SocketIOMessage.FrameType.HEARTBEAT;
        var delay = new WaitForSeconds(heartbeatTimeout * 0.8f);

        while (socket.connected)
        {
            socket.Send(beat.ToString());
            yield return(delay);
        }
    }
Example #7
0
 /// <summary>
 /// Send a raw SocketIOMessage to the server.
 /// </summary>
 /// <param name='msg'>
 /// Message.
 /// </param>
 public int Send(SocketIOMessage msg)
 {
     msg.id = msgUid++;
     if (socket == null)
     {
         Debug.LogError("Socket.IO is not initialised yet!");
         return(-1);
     }
     else
     {
         socket.Send(msg.ToString());
         return(msg.id.Value);
     }
 }
Example #8
0
    public static SocketIOMessage FromString(string msg)
    {
        var m = new SocketIOMessage();
        var t = 0;

        if (int.TryParse(NextPart(msg, out msg), out t))
        {
            m.type = (FrameType)t;
        }
        var id = NextPart(msg, out msg);

        if (id == null)
        {
            m.id     = null;
            m.isUser = false;
        }
        else
        {
            if (id.EndsWith("+"))
            {
                m.isUser = true;
                id       = id.Substring(0, id.Length - 1);
            }
            int i;
            if (int.TryParse(id, out i))
            {
                m.id = i;
            }
        }
        m.endPoint = NextPart(msg, out msg);
        if (msg.Length > 0)
        {
            m.data = msg.Substring(1);
        }

        return(m);
    }
Example #9
0
 void HandleNewsEvent(SocketIOConnection socket, SocketIOMessage msg)
 {
     Debug.Log("Received Event: " + msg.eventName + "(" + msg.args + ")");
 }
Example #10
0
    void HandleSocketOnTextMessageRecv(string message)
    {
        var msg = SocketIOMessage.FromString(message);

        msg.socket = this;

        switch (msg.type)
        {
        case SocketIOMessage.FrameType.DISCONNECT:
            StopCoroutine("Hearbeat");
            if (handler != null)
            {
                if (handler.OnDisconnect != null)
                {
                    handler.OnDisconnect(msg);
                }
            }
            break;

        case SocketIOMessage.FrameType.CONNECT:
            if (msg.endPoint == null)
            {
                StartCoroutine("Heartbeat");
            }
            if (handler.OnConnect != null)
            {
                handler.OnConnect(msg);
            }
            break;

        case SocketIOMessage.FrameType.HEARTBEAT:
            if (handler.OnHearbeat != null)
            {
                handler.OnHearbeat(msg);
            }
            break;

        case SocketIOMessage.FrameType.MESSAGE:
            if (handler.OnMessage != null)
            {
                handler.OnMessage(msg);
            }
            break;

        case SocketIOMessage.FrameType.JSONMESSAGE:
            if (handler.OnJSONMessage != null)
            {
                var o = HTTP.JsonSerializer.Decode(msg.data);
                handler.OnJSONMessage(msg, o);
            }
            break;

        case SocketIOMessage.FrameType.EVENT:
            if (handler.OnEvent != null)
            {
                var o = HTTP.JsonSerializer.Decode(msg.data) as Hashtable;
                handler.OnEvent(msg, o["name"] as string, o["args"] as ArrayList);
            }
            break;

        case SocketIOMessage.FrameType.ACK:
            if (handler.OnACK != null)
            {
                handler.OnACK(msg);
            }
            break;

        case SocketIOMessage.FrameType.ERROR:
            if (handler.OnError != null)
            {
                handler.OnError(msg);
            }
            break;

        case SocketIOMessage.FrameType.NOOP:
            if (handler.OnNoop != null)
            {
                handler.OnNoop(msg);
            }
            break;

        default:
            break;
        }
    }
Example #11
0
 void OnEvent(SocketIOMessage msg, string name, ArrayList args)
 {
     Debug.Log("Event Received: " + name);
 }
Example #12
0
 void OnConnect(SocketIOMessage msg)
 {
     Debug.Log("Socket.IO connection established.");
 }
 void OnEvent(SocketIOMessage msg, string name, ArrayList args)
 {
     Debug.Log ("Event Received: " + name);
 }
 void OnConnect(SocketIOMessage msg)
 {
     Debug.Log("Socket.IO connection established.");
 }
 /// <summary>
 /// Send the specified payload as a JSON message to the server.
 /// </summary>
 /// <param name='payload'>
 /// Payload.
 /// </param>
 public int Send(object payload)
 {
     var m = new SocketIOMessage();
     m.type = SocketIOMessage.FrameType.JSONMESSAGE;
     m.data = HTTP.JsonSerializer.Encode(payload);
     return Send (m);
 }
 IEnumerator Heartbeat()
 {
     var beat = new SocketIOMessage ();
     beat.type = SocketIOMessage.FrameType.HEARTBEAT;
     var delay = new WaitForSeconds (heartbeatTimeout * 0.8f);
     while (socket.connected) {
         socket.Send (beat.ToString ());
         yield return delay;
     }
 }