Exemple #1
0
        private void close(PayloadData payload, bool send, bool wait)
        {
            lock (_forConn)
            {
                if (_readyState == WebSocketState.Closing || _readyState == WebSocketState.Closed)
                {
                    return;
                }

                _readyState = WebSocketState.Closing;
            }

            var e = new CloseEventArgs(payload);

            e.WasClean =
                closeHandshake(
                    send ? WebSocketFrame.CreateCloseFrame(Mask.Unmask, payload).ToByteArray() : null,
                    wait ? 1000 : 0,
                    closeServerResources);

            _readyState = WebSocketState.Closed;
            try
            {
                OnClose.Emit(this, e);
            }
            catch (Exception ex)
            {
                error("An exception has occurred while OnClose.", ex);
            }
        }
Exemple #2
0
        private async Task CloseAsync(PayloadData payload, bool send, bool wait)
        {
            lock (_forConn)
            {
                if (_readyState == WebSocketState.CloseSent || _readyState == WebSocketState.Closed)
                {
                    return;
                }

                _readyState = WebSocketState.CloseSent;
            }

            var e = new CloseEventArgs(payload)
            {
                WasClean = await CloseHandshakeAsync(
                    send?WebSocketFrame.CreateCloseFrame(Mask.Unmask, payload).ToByteArray() : null,
                        wait? 1000 : 0).ConfigureAwait(false)
            };

            _readyState = WebSocketState.Closed;
            try
            {
                OnClose.Emit(this, e);
            }
            catch (Exception ex)
            {
                error("An exception has occurred while OnClose.", ex);
            }
        }
Exemple #3
0
        private void onClose(CloseEventArgs eventArgs)
        {
            if (!Thread.CurrentThread.IsBackground)
            {
                if (!_exitMessageLoop.IsNull())
                {
                    _exitMessageLoop.WaitOne(5 * 1000);
                }
            }

            if (!closeResources())
            {
                eventArgs.WasClean = false;
            }

            OnClose.Emit(this, eventArgs);
        }
Exemple #4
0
        private void onClose(CloseEventArgs eventArgs)
        {
            if (!Thread.CurrentThread.IsBackground)
            {
                if (!_exitMessageLoop.IsNull())
                {
                    _exitMessageLoop.WaitOne(5 * 1000);
                }
            }

            if (closeConnection())
            {
                eventArgs.WasClean = true;
            }

            OnClose.Emit(this, eventArgs);
        }
        private void createWebSocket(string uri)
        {
            socket = new WebSocket(uri);
            if (Trace)
            {
                socket.Log.Level = LogLevel.Debug;
            }

            socket.OnClose += (sender, evt) =>
            {
                // Release socket handle
                socket = null;
                Logger.TraceIf(Trace, String.Format("Socket Closed. Code={0}, Reason={1}", evt.Code, evt.Reason));
                OnClose.Emit(this, new WebSocketCloseEventArgs(evt.Code, evt.Reason));
            };
            socket.OnMessage += (sender, evt) =>
            {
                if (evt.IsPing)
                {
                    Logger.TraceIf(Trace, "SocketReceive: WebSocket ping.");
                    return;
                }

                if (evt.IsText)
                {
                    Logger.TraceIf(Trace, "SocketReceive: Invalid content (text/plain).");
                    return;
                }

                OnMessage.Emit(this, new WebSocketMessageEventArgs(evt.RawData));
            };
            socket.OnError += (sender, evt) =>
            {
                if (OnError != null)
                {
                    OnError.Emit(sender, new WebSocketErrorEventArgs(evt.Exception));
                }
            };
            socket.OnOpen += (sender, evt) =>
            {
                if (OnOpen != null)
                {
                    OnOpen.Emit(sender, evt);
                }
            };
        }