예제 #1
0
        /// <summary>
        /// Last call of the OnPacket chain(Transport -> Manager -> Socket), we will dispatch the event if there is any callback
        /// </summary>
        void ISocket.OnPacket(Packet packet)
        {
            // Some preprocessing of the the packet
            switch (packet.SocketIOEvent)
            {
            case SocketIOEventTypes.Disconnect:
                if (IsOpen)
                {
                    IsOpen = false;
                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Disconnect), packet);
                    Disconnect();
                }
                break;

            // Create an Error object from the server-sent json string
            case SocketIOEventTypes.Error:
                bool success = false;
                var  errDict = JSON.Json.Decode(packet.Payload, ref success) as Dictionary <string, object>;
                if (success)
                {
                    Error err = new Error((SocketIOErrors)Convert.ToInt32(errDict["code"]),
                                          errDict["message"] as string);

                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, err);

                    return;
                }
                break;
            }

            // Dispatch the event to all subscriber
            EventCallbacks.Call(packet);

            // call Ack callbacks
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && AckCallbacks != null)
            {
                SocketIOAckCallback ackCallback = null;
                if (AckCallbacks.TryGetValue(packet.Id, out ackCallback) &&
                    ackCallback != null)
                {
                    try
                    {
                        ackCallback(this, packet, packet.Decode(Manager.Encoder));
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }

                AckCallbacks.Remove(packet.Id);
            }
        }
예제 #2
0
        void ISocket.OnPacket(Packet packet)
        {
            switch (packet.SocketIOEvent)
            {
            case SocketIOEventTypes.Disconnect:
                if (this.IsOpen)
                {
                    this.IsOpen = false;
                    this.Disconnect();
                }
                break;

            case SocketIOEventTypes.Error:
            {
                bool flag = false;
                Dictionary <string, object> dictionary = Json.Decode(packet.Payload, ref flag) as Dictionary <string, object>;
                if (flag)
                {
                    Error error = new Error((SocketIOErrors)Convert.ToInt32(dictionary["code"]), dictionary["message"] as string);
                    this.EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, new object[]
                        {
                            error
                        });
                    return;
                }
                break;
            }
            }
            this.EventCallbacks.Call(packet);
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && this.AckCallbacks != null)
            {
                SocketIOAckCallback socketIOAckCallback = null;
                if (this.AckCallbacks.TryGetValue(packet.Id, out socketIOAckCallback) && socketIOAckCallback != null)
                {
                    try
                    {
                        socketIOAckCallback(this, packet, packet.Decode(this.Manager.Encoder));
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }
                this.AckCallbacks.Remove(packet.Id);
            }
        }
예제 #3
0
        /// <summary>
        /// Last call of the OnPacket chain(Transport -> Manager -> Socket), we will dispatch the event if there is any callback
        /// </summary>
        void ISocket.OnPacket(Packet packet)
        {
            // Some preprocessing of the packet
            switch (packet.SocketIOEvent)
            {
            case SocketIOEventTypes.Connect:
                this.Id = this.Namespace != "/" ? this.Namespace + "#" + this.Manager.Handshake.Sid : this.Manager.Handshake.Sid;
                break;

            case SocketIOEventTypes.Disconnect:
                if (IsOpen)
                {
                    IsOpen = false;
                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Disconnect), packet);
                    Disconnect();
                }
                break;

            // Create an Error object from the server-sent json string
            case SocketIOEventTypes.Error:
                bool   success = false;
                object result  = JSON.Json.Decode(packet.Payload, ref success);
                if (success)
                {
                    var   errDict = result as Dictionary <string, object>;
                    Error err;

                    if (errDict != null && errDict.ContainsKey("code"))
                    {
                        err = new Error((SocketIOErrors)Convert.ToInt32(errDict["code"]), errDict["message"] as string);
                    }
                    else
                    {
                        err = new Error(SocketIOErrors.Custom, packet.Payload);
                    }

                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, err);

                    return;
                }
                break;
            }

            // Dispatch the event to all subscriber
            EventCallbacks.Call(packet);

            // call Ack callbacks
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && AckCallbacks != null)
            {
                SocketIOAckCallback ackCallback = null;
                if (AckCallbacks.TryGetValue(packet.Id, out ackCallback) &&
                    ackCallback != null)
                {
                    try
                    {
                        ackCallback(this, packet, this.AutoDecodePayload ? packet.Decode(Manager.Encoder) : null);
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }

                AckCallbacks.Remove(packet.Id);
            }
        }
예제 #4
0
파일: Socket.cs 프로젝트: ubberkid/PeerATT
        /// <summary>
        /// Last call of the OnPacket chain(Transport -> Manager -> Socket), we will dispatch the event if there is any callback
        /// </summary>
        void ISocket.OnPacket(Packet packet)
        {
            // Some preprocessing of the the packet
            switch(packet.SocketIOEvent)
            {
                case SocketIOEventTypes.Disconnect:
                    if (IsOpen)
                    {
                        IsOpen = false;
                        EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Disconnect), packet);
                        Disconnect();
                    }
                    break;

                // Create an Error object from the server-sent json string
                case SocketIOEventTypes.Error:
                    bool success = false;
                    var errDict = JSON.Json.Decode(packet.Payload, ref success) as Dictionary<string, object>;
                    if (success)
                    {
                        Error err = new Error((SocketIOErrors)Convert.ToInt32(errDict["code"]),
                                                                              errDict["message"] as string);

                        EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, err);

                        return;
                    }
                    break;
            }

            // Dispatch the event to all subscriber
            EventCallbacks.Call(packet);

            // call Ack callbacks
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && AckCallbacks != null)
            {
                SocketIOAckCallback ackCallback = null;
                if (AckCallbacks.TryGetValue(packet.Id, out ackCallback) && 
                    ackCallback != null)
                {
                    try
                    {
                        ackCallback(this, packet, packet.Decode(Manager.Encoder));
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }

                AckCallbacks.Remove(packet.Id);
            }
        }
예제 #5
0
        /// <summary>
        /// Last call of the OnPacket chain(Transport -> Manager -> Socket), we will dispatch the event if there is any callback
        /// </summary>
        void ISocket.OnPacket(Packet packet)
        {
            // Some preprocessing of the packet
            switch (packet.SocketIOEvent)
            {
            case SocketIOEventTypes.Connect:
                if (this.Manager.Options.ServerVersion != SupportedSocketIOVersions.v3)
                {
                    this.Id = this.Namespace != "/" ? this.Namespace + "#" + this.Manager.Handshake.Sid : this.Manager.Handshake.Sid;
                }
                else
                {
                    var data = JSON.Json.Decode(packet.Payload) as Dictionary <string, object>;
                    this.Id = data["sid"].ToString();
                }
                this.IsOpen = true;
                break;

            case SocketIOEventTypes.Disconnect:
                if (IsOpen)
                {
                    IsOpen = false;
                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Disconnect), packet);
                    Disconnect();
                }
                break;

            // Create an Error object from the server-sent json string
            case SocketIOEventTypes.Error:
                bool   success = false;
                object result  = JSON.Json.Decode(packet.Payload, ref success);
                if (success)
                {
                    var   errDict = result as Dictionary <string, object>;
                    Error err     = null;

                    if (errDict != null)
                    {
                        object tmpObject = null;
                        string code      = null;
                        if (errDict.TryGetValue("code", out tmpObject))
                        {
                            code = tmpObject.ToString();
                        }

                        int errorCode;
                        if (code != null && int.TryParse(code, out errorCode) && errorCode >= 0 && errorCode <= 7)
                        {
                            errDict.TryGetValue("message", out tmpObject);
                            err = new Error((SocketIOErrors)errorCode, tmpObject != null ? tmpObject.ToString() : string.Empty);
                        }
                    }

                    if (err == null)
                    {
                        err = new Error(SocketIOErrors.Custom, packet.Payload);
                    }

                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, err);

                    return;
                }
                break;
            }

            // Dispatch the event to all subscriber
            EventCallbacks.Call(packet);

            // call Ack callbacks
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && AckCallbacks != null)
            {
                SocketIOAckCallback ackCallback = null;
                if (AckCallbacks.TryGetValue(packet.Id, out ackCallback) &&
                    ackCallback != null)
                {
                    try
                    {
                        ackCallback(this, packet, this.AutoDecodePayload ? packet.Decode(Manager.Encoder) : null);
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }

                AckCallbacks.Remove(packet.Id);
            }
        }