Exemple #1
0
        public void Connect(string url, string path = "/socket.io", string nsp = "/")
        {
            if (_socket != null)
            {
                _socket.disconnect();
            }

            Url = url;
            Nsp = nsp;

            var options = new EngineIO.Options();

            _socket = new SocketIO(new WebSocketSharpImpl(), url, path, nsp, options);

            _socket
            .on("connect", data =>
            {
                _isConnected = true;
                if (EventConnectionChange != null)
                {
                    EventConnectionChange(this, new EventArgs <bool>(true));
                }
            })
            .on("disconnect", data =>
            {
                this._isConnected = false;
                if (EventConnectionChange != null)
                {
                    EventConnectionChange(this, new EventArgs <bool>(false));
                }
                if (_requestDisconnect)
                {
                    _requestDisconnect = false;
                    if (EventDisconnect != null)
                    {
                        EventDisconnect(this, new EventArgs());
                    }
                }
            });

            _socket.EventReceive += (s, e) =>
            {
                if (EventReceive != null)
                {
                    EventReceive(this, new EventArgs <EventData>(new EventData(e.Value.Name, e.Value.Data)));
                }

                if (_requestQueue.Contains(e.Value.Name))
                {
                    _requestQueue.Receive(e.Value.Name, e.Value.Data);
                }
            };
            if (EventNewSocketInstance != null)
            {
                EventNewSocketInstance(this, new EventArgs());
            }
        }
Exemple #2
0
        public void Connect(string url, string nsp = "/")
        {
            if (_socket != null)
            {
                _socket.disconnect();
            }

            Url = url;
            Nsp = nsp;

            var options = new EngineIO.Options();

            _socket = new SocketIO(new WebSocketSharpImpl(), url, nsp, options);

            _socket
            .on("connect", data =>
            {
                _isConnected = true;
                if (EventConnectionChange != null)
                {
                    EventConnectionChange(this, new EventArgs <bool>(true));
                }
            })
            .on("disconnect", data =>
            {
                this._isConnected = false;
                if (EventConnectionChange != null)
                {
                    EventConnectionChange(this, new EventArgs <bool>(false));
                }
                if (_requestDisconnect)
                {
                    _requestDisconnect = false;
                    if (EventDisconnect != null)
                    {
                        EventDisconnect(this, new EventArgs());
                    }
                }
            })
            .on("reconnecting", data =>
            {
                if (EventReconnecting != null)
                {
                    EventReconnecting(this, new EventArgs <int>((int)data));
                }
            })
            .on("reconnect", data =>
            {
                this._isConnected = false;
                if (EventReconnected != null)
                {
                    EventReconnected(this, new EventArgs <int>((int)data));
                }
            })
            .on("exception", data =>
            {
                var error = (data as JToken).ToObject <NestJSWSException>();
                if (EventNestJSException != null)
                {
                    EventNestJSException(this, new EventArgs <NestJSWSException>(error));
                }
            });

            _socket.EventReceive += (s, e) =>
            {
                if (EventReceive != null)
                {
                    EventReceive(this, new EventArgs <EventData>(new EventData(e.Value.Name, e.Value.Data)));
                }

                if (_requestQueue.Contains(e.Value.Name))
                {
                    _requestQueue.Receive(e.Value.Name, e.Value.Data);
                }
            };
            if (EventNewSocketInstance != null)
            {
                EventNewSocketInstance(this, new EventArgs());
            }
        }