protected override void OnCloseConnection(WebSocketCloseReason aReason) { if (close != null) { close(this, aReason); } }
/// <summary> /// 引发关闭完成事件 /// </summary> /// <param name="reason"></param> protected void OnClosed(WebSocketCloseReason reason) { if (this.Closed != null) { this.Closed(this, new WebSocketCloseEventArgs(reason)); } }
protected virtual void OnCloseConnection(WebSocketCloseReason aReason) { if (close != null) { close(this, aReason); } }
public void ProcessOnClose(WebSocketCloseReason aCloseReason) { Deployment.Current.Dispatcher.BeginInvoke(() => { StatusList.Items.Add("Disconnected!"); PingBtn.IsEnabled = false; DisconnectBtn.IsEnabled = false; Echo.IsEnabled = false; }); }
public async Task CloseAsync(WebSocketCloseReason reason) { if (Interlocked.CompareExchange(ref this.closeState, WS_STATE_CLOSE_SENT, WS_STATE_OPEN) != WS_STATE_OPEN && Interlocked.CompareExchange(ref this.closeState, WS_STATE_CLOSED, WS_STATE_CLOSE_RECEIVED) != WS_STATE_CLOSE_RECEIVED) { return; } if (this.log.IsDebugEnabled) { this.log.Debug($"A close is called on websocket. Switching state to '{(this.closeState == WS_STATE_CLOSE_SENT ? "CloseSent" : "Closed")}'."); } await this.writeSemaphore.WaitAsync().ConfigureAwait(false); try { EndianBitConverter.UInt16CopyBytesBe((ushort)reason, this.outCloseBuffer.Array, this.outCloseBuffer.Offset); var messageType = (WebSocketMessageType)WebSocketFrameOption.ConnectionClose; var closeFrame = this.PrepareFrame(this.outCloseBuffer, 2, true, false, messageType, WebSocketExtensionFlags.None); await this.SendFrameAsync(closeFrame, Timeout.InfiniteTimeSpan, SendOptions.NoLock | SendOptions.NoErrors | SendOptions.IgnoreClose, CancellationToken.None).ConfigureAwait(false); await this.networkConnection.FlushAsync(CancellationToken.None).ConfigureAwait(false); if (this.closeState >= WS_STATE_CLOSED) { await this.networkConnection.CloseAsync().ConfigureAwait(false); } } catch (Exception closeError) when(closeError.Unwrap() is ThreadAbortException == false) { var closeErrorUnwrap = closeError.Unwrap(); if (closeErrorUnwrap is IOException || closeErrorUnwrap is OperationCanceledException || closeErrorUnwrap is InvalidOperationException) { return; // ignore common IO exceptions while closing connection } if (this.log.IsDebugEnabled) { this.log.Debug($"({this.GetHashCode():X}) An error occurred while closing connection.", closeError.Unwrap()); } } finally { SafeEnd.ReleaseSemaphore(this.writeSemaphore, this.log); } }
/// <summary> /// 关闭连接 /// </summary> protected void Close(WebSocketCloseReason reason) { lock (this.sendLockObject) { if (this.isConnectioned == true) { this.isConnectioned = false; if (reason == WebSocketCloseReason.Close) { this.autoConnect = false; } this.stream.Close(); this.socket.Close(); // this.OnClosed(reason); } } }
private void StopIt() { mIsRunning = false; mStatus = WebSocketStatus.CLOSED; mCLose = WebSocketCloseReason.CLIENT; try { mNetStream.Close(); mHeaders.GetRequestFields.Clear(); mHeaders.GetResponseFields.Clear(); } catch (IOException lIOEx) { if (mLog.IsErrorEnabled) { mLog.Error(lIOEx.Message); } OnError(new WebSocketError(lIOEx.Message)); throw new WebSocketException(lIOEx.Message); } }
/// <summary> /// 关闭当前连接 /// </summary> /// <param name="reason"></param> /// <param name="message"></param> protected void Close(string message, WebSocketCloseReason reason) { var trigger = false; lock (this) { if (this.isClosed == false) { this.socket.Close(); this.closeMessage = message; trigger = true; this.isClosed = true; } } if (trigger) { this.Server.OnWebSocketDisconnected(this); } }
/// <summary> /// 初始化新实例 /// </summary> /// <param name="reason"></param> public WebSocketCloseEventArgs(WebSocketCloseReason reason) { this.Reason = reason; }
private void ProcessHybi() { WebSocketFrameType lFrameType; IWebSocketPacket lPacket; while (mIsRunning) { try { lPacket = WebSocketProtocolAbstraction.protocolToRawPacket(mNetStream); Console.WriteLine(lPacket.GetString()); lFrameType = (lPacket != null ? lPacket.FrameType : WebSocketFrameType.INVALID); if (lFrameType.Equals(null)) { if (mLog.IsErrorEnabled) { mLog.Error(WebSocketMessage.NULL_FRAME_TYPE); } mIsRunning = false; mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.NULL_FRAME_TYPE, mCLose)); } else if (lFrameType.Equals(WebSocketFrameType.INVALID)) { if (mLog.IsErrorEnabled) { mLog.Error(WebSocketMessage.INVALID_HYBI_FRAME); } mIsRunning = false; mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.INVALID_HYBI_FRAME, mCLose)); } else if (lFrameType.Equals(WebSocketFrameType.CLOSE)) { if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.CLOSE_FRAME_TYPE); } mIsRunning = false; mCLose = WebSocketCloseReason.CLIENT; StopIt(); } else if (lFrameType.Equals(WebSocketFrameType.PONG)) { OnRecivePong(); if (WebSocketStateOfStatus.isWritable(mStatus)) { if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.PING_PACKET_SENDING); } Ping(); OnSendPing(); } } else if (lFrameType.Equals(WebSocketFrameType.BINARY)) { if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.BINARY_PACKET_RECEIVED); } //****TODO**** } else if (lFrameType.Equals(WebSocketFrameType.TEXT)) { if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.TEXT_PACKET_RECEIVED); } OnReciveTextPacket(this, lPacket); } else if (lFrameType.Equals(WebSocketFrameType.FRAGMENT)) { if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.FRAGMENT_PACKET_RECEIVED); } //****TODO**** } } catch (Exception lEx) { mIsRunning = false; if (mStatus.Equals(WebSocketStatus.CLOSED)) { mCLose = WebSocketCloseReason.CLIENT; } else { mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.ERROR_HYBI_PROCESSOR, mCLose)); if (mLog.IsErrorEnabled) { mLog.Error(WebSocketMessage.ERROR_HYBI_PROCESSOR + WebSocketMessage.SEPARATOR + lEx.Message); } } } } }
public void Open(string aURI, string aSubProtocol, int aTimeout, int aVersion) { try { if (mLog.IsDebugEnabled) { mLog.Debug(WebSocketMessage.ESTABLiSHING_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.URL + aURI + WebSocketMessage.SEPARATOR + WebSocketMessage.SUBPROTOCOL + aSubProtocol + WebSocketMessage.SEPARATOR + WebSocketMessage.TIMEOUT + aTimeout + WebSocketMessage.SEPARATOR + WebSocketMessage.VERSION + aVersion); } mStatus = WebSocketStatus.OPENING; mVersion = aVersion; mURI = new Uri(aURI); WebSocketHandshake lHandshake = new WebSocketHandshake(mURI, aSubProtocol, mVersion, mCookieManage); if (mSocket != null && mSocket.Connected) { mSocket.Close(); } CreateSocket(); byte[] sendBuffer = lHandshake.GenerateC2SRequest(); mNetStream.Write(sendBuffer, 0, sendBuffer.Length); mNetStream.Flush(); mStatus = WebSocketStatus.CONNECTING; mHeaders.ReadRequestFromBuffer(sendBuffer); if (mLog.IsDebugEnabled) { mLog.Debug(WebSocketMessage.SENDING_HANDSHAKE + mHeaders.ToStringRequest()); } WebSocketTimeout.CallWithTimeout(mHeaders.ReadResponseFromStream, aTimeout, mNetStream); mCookieManage.AddCookies(mHeaders.GetCookies, mURI); if (mLog.IsDebugEnabled) { mLog.Debug(WebSocketMessage.RECEIVING_HANDSHAKE + mHeaders.ToStringResponse()); } lHandshake.VerifyS2CResponse(mHeaders); string lProtocol = GetResponseHeaderField(WebSocketConstants.SEC_WEBSOCKET_PROTOCOL); if (lProtocol != null) { mNegotiatedSubProtocol = new WebSocketSubProtocol(lProtocol, mEncoding); } else { mNegotiatedSubProtocol = new WebSocketSubProtocol(WebSocketConstants.WS_SUBPROT_DEFAULT, WebSocketConstants.WS_ENCODING_DEFAULT); } Thread lReciver = new Thread(new ThreadStart(Receiver)); lReciver.Start(); if (mLog.IsInfoEnabled) { mLog.Info(WebSocketMessage.CONNECTION_HAS_BEEN_ESTABLISHED); } mStatus = WebSocketStatus.OPENED; mIsRunning = true; OnOpenConnection(mHeaders); } catch (TimeoutException lTe) { if (mLog.IsErrorEnabled) { mLog.Error(WebSocketMessage.EXCEEDED_FOR_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.DETAILS + lTe.Message); } mCLose = WebSocketCloseReason.TIMEOUT; OnCloseConnection(mCLose); OnError(new WebSocketError(WebSocketMessage.TIMEOUT + WebSocketMessage.EXCEEDED_FOR_CONNECTION, mCLose)); CheckReconnect(); } catch (Exception lEx) { if (mLog.IsErrorEnabled) { mLog.Error(WebSocketMessage.NOT_ESTABLISH_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.DETAILS + lEx.Message); } mCLose = WebSocketCloseReason.BROKEN; OnCloseConnection(mCLose); OnError(new WebSocketError(WebSocketMessage.NOT_ESTABLISH_CONNECTION, mCLose)); CheckReconnect(); } }
private void Close(WebSocketTokenClient sender, WebSocketCloseReason e) { gb_send.Enabled = false; listBox1.Items.Add("Connection Closed : " + e.ToString()); }
public abstract Task CloseAsync(WebSocketCloseReason closeCode);
public void Open(string aURI, string aSubProtocol, int aTimeout, int aVersion) { try { if (mLog.IsDebugEnabled) mLog.Debug(WebSocketMessage.ESTABLiSHING_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.URL + aURI + WebSocketMessage.SEPARATOR + WebSocketMessage.SUBPROTOCOL + aSubProtocol + WebSocketMessage.SEPARATOR + WebSocketMessage.TIMEOUT + aTimeout + WebSocketMessage.SEPARATOR + WebSocketMessage.VERSION + aVersion); mStatus = WebSocketStatus.OPENING; mVersion = aVersion; mURI = new Uri(aURI); WebSocketHandshake lHandshake = new WebSocketHandshake(mURI, aSubProtocol, mVersion,mCookieManage); if (mSocket != null && mSocket.Connected) mSocket.Close(); CreateSocket(); byte[] sendBuffer = lHandshake.GenerateC2SRequest(); mNetStream.Write(sendBuffer, 0, sendBuffer.Length); mNetStream.Flush(); mStatus = WebSocketStatus.CONNECTING; mHeaders.ReadRequestFromBuffer(sendBuffer); if (mLog.IsDebugEnabled) mLog.Debug(WebSocketMessage.SENDING_HANDSHAKE + mHeaders.ToStringRequest()); WebSocketTimeout.CallWithTimeout(mHeaders.ReadResponseFromStream, aTimeout, mNetStream); mCookieManage.AddCookies(mHeaders.GetCookies, mURI); if (mLog.IsDebugEnabled) mLog.Debug(WebSocketMessage.RECEIVING_HANDSHAKE + mHeaders.ToStringResponse()); lHandshake.VerifyS2CResponse(mHeaders); string lProtocol = GetResponseHeaderField(WebSocketConstants.SEC_WEBSOCKET_PROTOCOL); if (lProtocol != null) mNegotiatedSubProtocol = new WebSocketSubProtocol(lProtocol, mEncoding); else mNegotiatedSubProtocol = new WebSocketSubProtocol(WebSocketConstants.WS_SUBPROT_DEFAULT, WebSocketConstants.WS_ENCODING_DEFAULT); Thread lReciver = new Thread(new ThreadStart(Receiver)); lReciver.Start(); if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.CONNECTION_HAS_BEEN_ESTABLISHED); mStatus = WebSocketStatus.OPENED; mIsRunning = true; OnOpenConnection(mHeaders); } catch (TimeoutException lTe) { if (mLog.IsErrorEnabled) mLog.Error(WebSocketMessage.EXCEEDED_FOR_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.DETAILS + lTe.Message); mCLose = WebSocketCloseReason.TIMEOUT; OnCloseConnection(mCLose); OnError(new WebSocketError(WebSocketMessage.TIMEOUT + WebSocketMessage.EXCEEDED_FOR_CONNECTION, mCLose)); CheckReconnect(); } catch (Exception lEx) { if (mLog.IsErrorEnabled) mLog.Error(WebSocketMessage.NOT_ESTABLISH_CONNECTION + WebSocketMessage.SEPARATOR + WebSocketMessage.DETAILS + lEx.Message); mCLose = WebSocketCloseReason.BROKEN; OnCloseConnection(mCLose); OnError(new WebSocketError(WebSocketMessage.NOT_ESTABLISH_CONNECTION, mCLose)); CheckReconnect(); } }
public override Task CloseAsync(WebSocketCloseReason closeReason) { return(this.Connection.CloseAsync(closeReason)); }
private void Close(WebSocketTokenClient sender, WebSocketCloseReason e) { gb_send.Enabled = false; listBox1.Items.Add("Connection Closed : " + e.ToString()); }
public void OnWebSocketClosed(DateTime timestamp, DateTime establishedTime, WebSocketCloseReason closeReason, long messagesRead, long messagesWritten) { Telemetry = new WebSocketsTelemetry(timestamp, establishedTime, closeReason, messagesRead, messagesWritten); }
public WebSocketError(string aReason, WebSocketCloseReason aCloseReason) { mReason = aReason; mCloseReason = aCloseReason; }
private void StopIt() { mIsRunning = false; mStatus = WebSocketStatus.CLOSED; mCLose = WebSocketCloseReason.CLIENT; try { mNetStream.Close(); mHeaders.GetRequestFields.Clear(); mHeaders.GetResponseFields.Clear(); } catch (IOException lIOEx) { if (mLog.IsErrorEnabled) mLog.Error(lIOEx.Message); OnError(new WebSocketError(lIOEx.Message)); throw new WebSocketException(lIOEx.Message); } }
private void ProcessHybi() { WebSocketFrameType lFrameType; IWebSocketPacket lPacket; while (mIsRunning) { try { lPacket = WebSocketProtocolAbstraction.protocolToRawPacket(mNetStream); Console.WriteLine(lPacket.GetString()); lFrameType = (lPacket != null ? lPacket.FrameType : WebSocketFrameType.INVALID); if (lFrameType.Equals(null)) { if (mLog.IsErrorEnabled) mLog.Error(WebSocketMessage.NULL_FRAME_TYPE); mIsRunning = false; mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.NULL_FRAME_TYPE, mCLose)); } else if (lFrameType.Equals(WebSocketFrameType.INVALID)) { if (mLog.IsErrorEnabled) mLog.Error(WebSocketMessage.INVALID_HYBI_FRAME); mIsRunning = false; mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.INVALID_HYBI_FRAME, mCLose)); } else if (lFrameType.Equals(WebSocketFrameType.CLOSE)) { if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.CLOSE_FRAME_TYPE); mIsRunning = false; mCLose = WebSocketCloseReason.CLIENT; StopIt(); } else if (lFrameType.Equals(WebSocketFrameType.PONG)) { OnRecivePong(); if (WebSocketStateOfStatus.isWritable(mStatus)) { if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.PING_PACKET_SENDING); Ping(); OnSendPing(); } } else if (lFrameType.Equals(WebSocketFrameType.BINARY)) { if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.BINARY_PACKET_RECEIVED); //****TODO**** } else if (lFrameType.Equals(WebSocketFrameType.TEXT)) { if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.TEXT_PACKET_RECEIVED); OnReciveTextPacket(this, lPacket); } else if (lFrameType.Equals(WebSocketFrameType.FRAGMENT)) { if (mLog.IsInfoEnabled) mLog.Info(WebSocketMessage.FRAGMENT_PACKET_RECEIVED); //****TODO**** } } catch (Exception lEx) { mIsRunning = false; if (mStatus.Equals(WebSocketStatus.CLOSED)) mCLose = WebSocketCloseReason.CLIENT; else { mCLose = WebSocketCloseReason.BROKEN; OnError(new WebSocketError(WebSocketMessage.ERROR_HYBI_PROCESSOR, mCLose)); if (mLog.IsErrorEnabled) mLog.Error(WebSocketMessage.ERROR_HYBI_PROCESSOR + WebSocketMessage.SEPARATOR + lEx.Message); } } } }
protected virtual void OnCloseConnection(WebSocketCloseReason aReason) { if (close != null) close(this, aReason); }
protected override void OnCloseConnection(WebSocketCloseReason aReason) { if (close != null) close(this, aReason); }
public void OnWebSocketClosed(DateTime timestamp, DateTime establishedTime, WebSocketCloseReason closeReason, long messagesRead, long messagesWritten) { _logger.LogInformation($"WebSocket connection closed ({closeReason}) after reading {messagesRead} and writing {messagesWritten} messages over {(timestamp - establishedTime).TotalSeconds:N2} seconds."); }
public WebSocketError(string aReason, WebSocketCloseReason aCloseReason) { mReason = aReason; mCloseReason = aCloseReason; }