Example #1
0
        internal Socket(Reactor.Web.Socket.Transport transport)
        {
            this.transport = transport;

            this.State = SocketState.Open;

            this.transport.OnOpen = () =>
            {
                if (this.OnOpen != null)
                {
                    this.OnOpen();
                }
            };

            this.transport.OnError = (exception) =>
            {
                if (this.OnError != null)
                {
                    this.OnError(exception);
                }
            };

            this.transport.OnClose = () =>
            {
                this.State = SocketState.Closed;

                this.Close();

                if (this.OnClose != null)
                {
                    this.OnClose();
                }
            };

            this.transport.OnMessage = (message) =>
            {
                if (this.OnMessage != null)
                {
                    this.OnMessage(message);
                }
            };
        }
Example #2
0
        internal Socket(Reactor.Web.Socket.Transport transport)
        {
            this.transport = transport;

            this.State = SocketState.Open;

            this.transport.OnOpen = () =>
            {
                if (this.OnOpen != null)
                {
                    this.OnOpen();
                }
            };

            this.transport.OnError = (exception) =>
            {
                if (this.OnError != null)
                {
                    this.OnError(exception);
                }
            };

            this.transport.OnClose = () =>
            {
                this.State = SocketState.Closed;

                this.Close();

                if (this.OnClose != null)
                {
                    this.OnClose();
                }
            };

            this.transport.OnMessage = (message) =>
            {
                if (this.OnMessage != null)
                {
                    this.OnMessage(message);
                }
            };
        }
Example #3
0
        internal Socket(string url, Dictionary <string, string> Headers)
        {
            var request = WebSocketRequest.Create(url);

            request.Headers = Headers;

            request.GetResponse((exception, response) => {
                //---------------------------------------
                // check for handshake error
                //---------------------------------------

                if (exception != null)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(exception);
                    }

                    return;
                }

                //---------------------------------------
                // check for non upgrade errors
                //---------------------------------------

                if (response.StatusCode != 101)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(new Exception("server rejected connection"));
                    }

                    return;
                }

                //---------------------------------------
                // configure events
                //---------------------------------------

                this.transport = new Transport(response.Socket);

                //---------------------------------------
                // emit open
                //---------------------------------------

                if (this.OnOpen != null)
                {
                    this.OnOpen();
                }

                this.transport.OnError += (error) =>
                {
                    if (this.OnError != null)
                    {
                        this.OnError(error);
                    }
                };

                this.transport.OnClose += () =>
                {
                    this.State = SocketState.Closed;

                    if (this.OnClose != null)
                    {
                        this.OnClose();
                    }
                };

                this.transport.OnMessage += (message) =>
                {
                    if (this.OnMessage != null)
                    {
                        this.OnMessage(message);
                    }
                };

                //--------------------------------------------
                // accept any frames passed on the response.
                //--------------------------------------------

                foreach (var frame in response.Frames)
                {
                    this.transport.AcceptFrame(frame);
                }
            });
        }
Example #4
0
        internal Socket(string url, Dictionary<string, string> Headers)
        {
            var request = WebSocketRequest.Create(url);

            request.Headers = Headers;

            request.GetResponse((exception, response) => {

                //---------------------------------------
                // check for handshake error
                //---------------------------------------

                if (exception != null)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(exception);
                    }

                    return;
                }

                //---------------------------------------
                // check for non upgrade errors
                //---------------------------------------

                if (response.StatusCode != 101)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(new Exception("server rejected connection"));
                    }

                    return;
                }

                //---------------------------------------
                // configure events
                //---------------------------------------

                this.transport = new Transport(response.Socket);

                //---------------------------------------
                // emit open
                //---------------------------------------

                if (this.OnOpen != null)
                {
                    this.OnOpen();
                }

                this.transport.OnError += (error) =>
                {
                    if (this.OnError != null)
                    {
                        this.OnError(error);
                    }
                };

                this.transport.OnClose += () =>
                {
                    this.State = SocketState.Closed;

                    if (this.OnClose != null)
                    {
                        this.OnClose();
                    }
                };

                this.transport.OnMessage += (message) =>
                {
                    if (this.OnMessage != null)
                    {
                        this.OnMessage(message);
                    }
                };

                //--------------------------------------------
                // accept any frames passed on the response.
                //--------------------------------------------

                foreach (var frame in response.Frames)
                {
                    this.transport.AcceptFrame(frame);
                }
            });
        }