Exemple #1
0
        public Socket Emit(string eventName, params object[] args)
        {
            bool blackListed = EventNames.IsBlacklisted(eventName);

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

            var packet = this.socket.Manager.Parser.CreateOutgoing(this.socket, SocketIOEventTypes.Event, this.id, eventName, args);

            packet.IsVolatile = this.isVolatile;
            (this.socket.Manager as IManager).SendPacket(packet);

            return(this.socket);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }