Beispiel #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);
            }
        }
Beispiel #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);
            }
        }
Beispiel #3
0
        void ISocket.OnPacket(Packet packet)
        {
            switch (packet.SocketIOEvent)
            {
            case SocketIOEventTypes.Disconnect:
                if (IsOpen)
                {
                    IsOpen = false;
                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Disconnect), packet);
                    Disconnect();
                }
                break;

            case SocketIOEventTypes.Error:
            {
                bool success = false;
                Dictionary <string, object> dictionary = Json.Decode(packet.Payload, ref success) as Dictionary <string, object>;
                if (success)
                {
                    Error error = new Error((SocketIOErrors)Convert.ToInt32(dictionary["code"]), dictionary["message"] as string);
                    EventCallbacks.Call(EventNames.GetNameFor(SocketIOEventTypes.Error), packet, error);
                    return;
                }
                break;
            }
            }
            EventCallbacks.Call(packet);
            if ((packet.SocketIOEvent == SocketIOEventTypes.Ack || packet.SocketIOEvent == SocketIOEventTypes.BinaryAck) && AckCallbacks != null)
            {
                SocketIOAckCallback value = null;
                if (AckCallbacks.TryGetValue(packet.Id, out value) && value != null)
                {
                    try
                    {
                        value(this, packet, packet.Decode(Manager.Encoder));
                    }
                    catch (Exception ex)
                    {
                        HTTPManager.Logger.Exception("Socket", "ackCallback", ex);
                    }
                }
                AckCallbacks.Remove(packet.Id);
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        public Socket Emit(string eventName, SocketIOAckCallback callback, params object[] args)
        {
            bool blackListed = EventNames.IsBlacklisted(eventName);

            if (blackListed)
            {
                throw new ArgumentException("Blacklisted event: " + eventName);
            }

            arguments.Clear();
            arguments.Add(eventName);

            // Find and swap any binary data(byte[]) to a placeholder string.
            // Server side these will be swapped back.
            List <byte[]> attachments = null;

            if (args != null && args.Length > 0)
            {
                int idx = 0;
                for (int i = 0; i < args.Length; ++i)
                {
                    byte[] binData = args[i] as byte[];
                    if (binData != null)
                    {
                        if (attachments == null)
                        {
                            attachments = new List <byte[]>();
                        }

                        Dictionary <string, object> placeholderObj = new Dictionary <string, object>(2);
                        placeholderObj.Add(Packet.Placeholder, true);
                        placeholderObj.Add("num", idx++);

                        arguments.Add(placeholderObj);

                        attachments.Add(binData);
                    }
                    else
                    {
                        arguments.Add(args[i]);
                    }
                }
            }

            string payload = null;

            try
            {
                payload = Manager.Encoder.Encode(arguments);
            }
            catch (Exception ex)
            {
                (this as ISocket).EmitError(SocketIOErrors.Internal, "Error while encoding payload: " + ex.Message + " " + ex.StackTrace);

                return(this);
            }

            // We don't use it further in this function, so we can clear it to not hold any unwanted reference.
            arguments.Clear();

            if (payload == null)
            {
                throw new ArgumentException("Encoding the arguments to JSON failed!");
            }

            int id = 0;

            if (callback != null)
            {
                id = Manager.NextAckId;

                if (AckCallbacks == null)
                {
                    AckCallbacks = new Dictionary <int, SocketIOAckCallback>();
                }

                AckCallbacks[id] = callback;
            }

            Packet packet = new Packet(TransportEventTypes.Message,
                                       attachments == null ? SocketIOEventTypes.Event : SocketIOEventTypes.BinaryEvent,
                                       this.Namespace,
                                       payload,
                                       0,
                                       id);

            if (attachments != null)
            {
                packet.Attachments = attachments; // This will set the AttachmentCount property too.
            }
            (Manager as IManager).SendPacket(packet);

            return(this);
        }
Beispiel #6
0
 public void CallServer(string eventName, SocketIOAckCallback callback = null, params object[] args)
 {
     socketManager.Socket.Emit(eventName, callback, args);
 }
Beispiel #7
0
        public Socket Emit(string eventName, SocketIOAckCallback callback, params object[] args)
        {
            bool blackListed = EventNames.IsBlacklisted(eventName);
            if (blackListed)
                throw new ArgumentException("Blacklisted event: " + eventName);

            arguments.Clear();
            arguments.Add(eventName);

            // Find and swap any binary data(byte[]) to a placeholder string.
            // Server side these will be swapped back.
            List<byte[]> attachments = null;
            if (args != null && args.Length > 0)
            {
                int idx = 0;
                for (int i = 0; i < args.Length; ++i)
                {
                    byte[] binData = args[i] as byte[];
                    if (binData != null)
                    {
                        if (attachments == null)
                            attachments = new List<byte[]>();

                        Dictionary<string, object> placeholderObj = new Dictionary<string, object>(2);
                        placeholderObj.Add(Packet.Placeholder, true);
                        placeholderObj.Add("num", idx++);

                        arguments.Add(placeholderObj);

                        attachments.Add(binData);
                    }
                    else
                        arguments.Add(args[i]);
                }
            }

            string payload = null;

            try
            {
                payload = Manager.Encoder.Encode(arguments);
            }
            catch(Exception ex)
            {
                (this as ISocket).EmitError(SocketIOErrors.Internal, "Error while encoding payload: " + ex.Message + " " + ex.StackTrace);

                return this;
            }

            // We don't use it further in this function, so we can clear it to not hold any unwanted reference.
            arguments.Clear();

            if (payload == null)
                throw new ArgumentException("Encoding the arguments to JSON failed!");

            int id = 0;

            if (callback != null)
            {
                id = Manager.NextAckId;

                if (AckCallbacks == null)
                    AckCallbacks = new Dictionary<int, SocketIOAckCallback>();

                AckCallbacks[id] = callback;
            }

            Packet packet = new Packet(TransportEventTypes.Message,
                                       attachments == null ? SocketIOEventTypes.Event : SocketIOEventTypes.BinaryEvent,
                                       this.Namespace,
                                       payload,
                                       0,
                                       id);

            if (attachments != null)
                packet.Attachments = attachments; // This will set the AttachmentCount property too.

            (Manager as IManager).SendPacket(packet);

            return this;
        }
Beispiel #8
0
        public Socket Emit(string eventName, SocketIOAckCallback callback, params object[] args)
        {
            if (EventNames.IsBlacklisted(eventName))
            {
                throw new ArgumentException("Blacklisted event: " + eventName);
            }
            arguments.Clear();
            arguments.Add(eventName);
            List <byte[]> list = null;

            if (args != null && args.Length > 0)
            {
                int num = 0;
                for (int i = 0; i < args.Length; i++)
                {
                    byte[] array = args[i] as byte[];
                    if (array != null)
                    {
                        if (list == null)
                        {
                            list = new List <byte[]>();
                        }
                        Dictionary <string, object> dictionary = new Dictionary <string, object>(2);
                        dictionary.Add("_placeholder", true);
                        dictionary.Add("num", num++);
                        arguments.Add(dictionary);
                        list.Add(array);
                    }
                    else
                    {
                        arguments.Add(args[i]);
                    }
                }
            }
            string text = null;

            try
            {
                text = Manager.Encoder.Encode(arguments);
            }
            catch (Exception ex)
            {
                ((ISocket)this).EmitError(SocketIOErrors.Internal, "Error while encoding payload: " + ex.Message + " " + ex.StackTrace);
                return(this);

                IL_011f :;
            }
            arguments.Clear();
            if (text == null)
            {
                throw new ArgumentException("Encoding the arguments to JSON failed!");
            }
            int num3 = 0;

            if (callback != null)
            {
                num3 = Manager.NextAckId;
                if (AckCallbacks == null)
                {
                    AckCallbacks = new Dictionary <int, SocketIOAckCallback>();
                }
                AckCallbacks[num3] = callback;
            }
            Packet packet = new Packet(TransportEventTypes.Message, (list != null) ? SocketIOEventTypes.BinaryEvent : SocketIOEventTypes.Event, Namespace, text, 0, num3);

            if (list != null)
            {
                packet.Attachments = list;
            }
            ((IManager)Manager).SendPacket(packet);
            return(this);
        }
Beispiel #9
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);
            }
        }
Beispiel #10
0
        public Socket Emit(string eventName, SocketIOAckCallback callback, params object[] args)
        {
            bool flag = EventNames.IsBlacklisted(eventName);

            if (flag)
            {
                throw new ArgumentException("Blacklisted event: " + eventName);
            }
            this.arguments.Clear();
            this.arguments.Add(eventName);
            List <byte[]> list = null;

            if (args != null && args.Length > 0)
            {
                int num = 0;
                for (int i = 0; i < args.Length; i++)
                {
                    byte[] array = args[i] as byte[];
                    if (array != null)
                    {
                        if (list == null)
                        {
                            list = new List <byte[]>();
                        }
                        this.arguments.Add(string.Format("{{\"_placeholder\":true,\"num\":{0}}}", num++.ToString()));
                        list.Add(array);
                    }
                    else
                    {
                        this.arguments.Add(args[i]);
                    }
                }
            }
            string text = null;

            try
            {
                text = this.Manager.Encoder.Encode(this.arguments);
            }
            catch (Exception ex)
            {
                ((ISocket)this).EmitError(SocketIOErrors.Internal, "Error while encoding payload: " + ex.Message + " " + ex.StackTrace);
                return(this);
            }
            this.arguments.Clear();
            if (text == null)
            {
                throw new ArgumentException("Encoding the arguments to JSON failed!");
            }
            int num2 = 0;

            if (callback != null)
            {
                num2 = this.Manager.NextAckId;
                if (this.AckCallbacks == null)
                {
                    this.AckCallbacks = new Dictionary <int, SocketIOAckCallback>();
                }
                this.AckCallbacks[num2] = callback;
            }
            Packet packet = new Packet(TransportEventTypes.Message, (list != null) ? SocketIOEventTypes.BinaryEvent : SocketIOEventTypes.Event, this.Namespace, text, 0, num2);

            if (list != null)
            {
                packet.Attachments = list;
            }
            ((IManager)this.Manager).SendPacket(packet);
            return(this);
        }