internal WebSocketException( CloseStatusCode code, string message, Exception innerException ) : base(message ?? code.GetMessage (), innerException) { _code = code; }
public void Disconnect(Action callback, CloseStatusCode code = CloseStatusCode.NoStatus, string reason = null) { if (_conn != null) { // _conn.OnClose(); //TODO how to clear event handler? if (code != CloseStatusCode.NoStatus) { _conn.Close(code, reason); } _conn = null; } if (callback != null) callback(); }
/// <summary> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>, /// and releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicate the status codes for closure. /// </param> public void Close(CloseStatusCode code) { var send = _readyState == WebSocketState.OPEN && !code.IsReserved (); close (new PayloadData (((ushort) code).ToByteArrayInternally (ByteOrder.BIG)), send, true); }
internal static string CheckCloseParameters (CloseStatusCode code, string reason, bool client) { return code == CloseStatusCode.NoStatus ? (!reason.IsNullOrEmpty () ? "NoStatus cannot have a reason." : null) : code == CloseStatusCode.MandatoryExtension && !client ? "MandatoryExtension cannot be used by a server." : code == CloseStatusCode.ServerError && client ? "ServerError cannot be used by a client." : !reason.IsNullOrEmpty () && reason.UTF8Encode ().Length > 123 ? "A reason has greater than the allowable max size." : null; }
/// <summary> /// Stops the current <see cref="WebSocketService"/> instance with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicates a status code /// indicating the reason for stop. /// </param> /// <param name="reason"> /// A <see cref="string"/> that contains the reason for stop. /// </param> public void Stop(CloseStatusCode code, string reason) { if (IsBound) _websocket.Close (code, reason); }
internal WebSocketException (CloseStatusCode code) : this (code, null, null) { }
public static WsFrame CreateCloseFrame(Mask mask, CloseStatusCode code, string reason) { return(new WsFrame(Opcode.Close, mask, new PayloadData(((ushort)code).Append(reason)))); }
internal WebSocketException (CloseStatusCode code, Exception innerException) : this (code, null, innerException) { }
protected Task CloseAsync(CloseStatusCode statusCode, string reason) { return(WebSocket.CloseAsync((int)statusCode, reason)); }
protected virtual Task OnDisconnected(CloseStatusCode statusCode, string reason) { return(Task.CompletedTask); }
private void Ws_OnClose(object sender, CloseEventArgs e) { WebSocket ws = (WebSocket)sender; if (_ws != ws) { return; } #region try { LastTryOn = DateTime.Now; CloseStatusCode closeStatus = CloseStatusCode.Undefined; if (Enum.IsDefined(typeof(CloseStatusCode), e.Code)) { closeStatus = (CloseStatusCode)e.Code; _closeCode = closeStatus.GetName(); } else { _closeCode = e.Code.ToString(); } Logger.InfoDebugLine($"Ws_OnClose {_closeCode} {e.Reason}"); switch (closeStatus) { case CloseStatusCode.Normal: _closeReason = e.Reason; if (e.Reason == WsMessage.ReGetServerAddress) { _closeReason = "服务器通知客户端重连"; // 3,收到服务器的WsMessage.ReGetServerAddress类型的消息时 NeedReWebSocket(); // 立即重连以防止在上一个连接关闭之前重连成功从而在界面上留下错误顺序的消息 NewWebSocket(); } else { // 正常关闭时,递增失败次数以增大重试延迟周期 IncreaseFailCount(); } break; case CloseStatusCode.Away: // 服务器ping不通时和服务器关闭进程时,递增失败次数以增大重试延迟周期 IncreaseFailCount(); _closeReason = "失去连接,请稍后重试"; // 可能是因为服务器节点不存在导致的失败,所以下一次进行重新获取服务器地址的全新连接 // 2,连不上服务器时 NeedReWebSocket(); break; case CloseStatusCode.ProtocolError: case CloseStatusCode.Undefined: // 协议错误导致连接关闭,递增失败次数以增大重试延迟周期 IncreaseFailCount(); // WebSocket协议并无定义状态码用于表达握手阶段身份验证失败的情况,阅读WebSocketSharp的源码发现验证不通过包含于ProtocolError中, // 姑且将ProtocolError视为用户名密码验证不通过,因为ProtocolError包含的其它失败情况在编程正确下不会发送。 _closeReason = "登录失败"; break; case CloseStatusCode.UnsupportedData: break; case CloseStatusCode.NoStatus: break; case CloseStatusCode.Abnormal: // 服务器或本机网络不可用时,递增失败次数以增大重试延迟周期 IncreaseFailCount(); _closeReason = "网络错误"; // 可能是因为服务器节点不存在导致的失败,所以下一次进行重新获取服务器地址的全新连接 // 2,连不上服务器时 NeedReWebSocket(); break; case CloseStatusCode.InvalidData: break; case CloseStatusCode.PolicyViolation: break; case CloseStatusCode.TooBig: break; case CloseStatusCode.MandatoryExtension: break; case CloseStatusCode.ServerError: break; case CloseStatusCode.TlsHandshakeFailure: break; default: break; } } catch { } NextTrySecondsDelayInit(); UpdateWsStateAsync($"{_closeCode} {_closeReason}", toOut: false); #endregion }
internal void Close(CloseStatusCode code = CloseStatusCode.Normal, string reason = null) { Context.WebSocket.Close(code, reason); }
public void SJd(CloseStatusCode code, string reason) { SJd((ushort)code, reason); }
protected override Task OnDisconnected(CloseStatusCode code, string reason) { Console.WriteLine($"{GetType().Name} Disconnected with code: " + code.ToString()); return(base.OnDisconnected(code, reason)); }
/// <summary> /// Stops receiving the WebSocket connection requests with the specified <see cref="CloseStatusCode"/> /// and <see cref="string"/>. /// </summary> /// <param name="code"> /// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for stop. /// </param> /// <param name="reason"> /// A <see cref="string"/> that contains the reason for stop. /// </param> public void Stop(CloseStatusCode code, string reason) { stop((ushort)code, reason); }
public CloseEventArgs(CloseStatusCode code) : this((ushort)code, null) { }
internal CloseEventArgs(CloseStatusCode code) : this((ushort)code, null, false) { }
/// <summary> /// Closes the connection and releases all associated resources after sends a Close control frame. /// </summary> /// <param name="code"> /// A <see cref="CloseStatusCode"/> that contains a status code indicating a reason for closure. /// </param> /// <param name="reason"> /// A <see cref="string"/> that contains a reason for closure. /// </param> public void Close(CloseStatusCode code, string reason) { Close((ushort)code, reason); }
internal WebSocketException(CloseStatusCode code) : this(code, code.GetMessage()) { }
private void close(CloseStatusCode code, string reason) { close((ushort)code, reason); }
internal WebSocketException(CloseStatusCode code, string reason) : this(code, reason, null) { }
/// <summary> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/> /// and <see cref="string"/>, and releases all associated resources. /// </summary> /// <remarks> /// This method emits a <see cref="OnError"/> event if the size /// of <paramref name="reason"/> is greater than 123 bytes. /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the close. /// </param> public void Close(CloseStatusCode code, string reason) { byte[] data = null; var msg = _readyState.CheckIfClosable() ?? (data = ((ushort)code).Append(reason)).CheckIfValidControlData("reason"); if (msg != null) { error(msg); return; } var send = _readyState == WebSocketState.Open && !code.IsReserved(); close(new PayloadData(data), send, send); }
internal CloseEventArgs (CloseStatusCode code) : this ((ushort) code) { }
private bool processUnsupportedFrame(WebSocketFrame frame, CloseStatusCode code, string reason) { processException(new WebSocketException(code, reason), null); return false; }
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/>, and releases all associated resources. /// </summary> /// <remarks> /// This method doesn't wait for the close to be complete. /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating /// the reason for closure. /// </param> public void CloseAsync(CloseStatusCode code) { CloseAsync (code, null); }
private void close(CloseStatusCode code, string reason, bool wait) { close (new PayloadData (((ushort) code).Append (reason)), !code.IsReserved (), wait); }
/// <inheritdoc /> public Task CloseAsync(CloseStatusCode code, string?comment = null, CancellationToken cancellationToken = default) => UnderlyingWebSocket.CloseAsync(MapCloseStatus(code), comment ?? string.Empty, cancellationToken);
/// <summary> /// Closes the session with the specified <paramref name="id"/>, <paramref name="code"/>, /// and <paramref name="reason"/>. /// </summary> /// <param name="id"> /// A <see cref="string"/> that represents the ID of the session to close. /// </param> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the close. /// </param> public void CloseSession(string id, CloseStatusCode code, string reason) { IWebSocketSession session; if (TryGetSession (id, out session)) session.Context.WebSocket.Close (code, reason); }
private WebSocketCloseStatus MapCloseStatus(CloseStatusCode code) => code switch {
/// <summary> /// Closes the WebSocket connection with the specified <paramref name="code"/> and /// <paramref name="reason"/>, and releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicates the status code for closure. /// </param> /// <param name="reason"> /// A <see cref="string"/> that contains the reason for closure. /// </param> public void Close(CloseStatusCode code, string reason) { close ((ushort) code, reason); }
internal WebSocketException(CloseStatusCode code) : this(code, null, null) { }
public CloseEventArgs(CloseStatusCode code, string reason) : this((ushort)code, reason) { }
internal WebSocketException(CloseStatusCode code, Exception innerException) : this(code, null, innerException) { }
internal CloseEventArgs(CloseStatusCode code, string reason, bool wasClean) : this((ushort)code, reason, wasClean) { }
internal WebSocketException(CloseStatusCode code, string message) : this(code, message, null) { }
protected override Task OnDisconnected(CloseStatusCode statusCode, string reason) { DisconnectedCalled = true; return(base.OnDisconnected(statusCode, reason)); }
internal WebSocketException(CloseStatusCode code, string message, Exception innerException) : base( message ?? code.GetMessage(), innerException) { Code = code; }
internal WebSocketException(CloseStatusCode code, string message) : base(message) { Code = code; }
private void close(CloseStatusCode code, string reason, bool wait) { close(new PayloadData(((ushort)code).Append(reason)), !code.IsReserved(), wait); }
/// <summary> /// Closes the WebSocket connection with the specified <paramref name="code"/> /// and releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that contains a status code /// indicating the reason for closure. /// </param> public void Close(CloseStatusCode code) { Close(code, String.Empty); }
private bool processUnsupportedFrame(WebSocketFrame frame, CloseStatusCode code, string reason) { processException(new WebSocketException(code, reason), null); return(false); }
internal WebSocketException( CloseStatusCode code, string reason, Exception innerException) : base(reason ?? code.GetMessage (), innerException) { Code = code; }
/// <summary> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>, /// and releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> public void Close(CloseStatusCode code) { var msg = _readyState.CheckIfClosable (); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); return; } var send = _readyState == WebSocketState.Open && !code.IsReserved (); close (new CloseEventArgs (code), send, send); }
internal WebSocketException (CloseStatusCode code, string message) : this (code, message, null) { }
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>, and releases /// all associated resources. /// </summary> /// <remarks> /// <para> /// This method doesn't wait for the close to be complete. /// </para> /// <para> /// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> /// is greater than 123 bytes. /// </para> /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the close. /// </param> public void CloseAsync(CloseStatusCode code, string reason) { CloseEventArgs e = null; var msg = _readyState.CheckIfClosable () ?? (e = new CloseEventArgs (code, reason)).RawData.CheckIfValidControlData ("reason"); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); return; } var send = _readyState == WebSocketState.Open && !code.IsReserved (); closeAsync (e, send, send); }
internal CloseEventArgs (CloseStatusCode code, string reason) : this ((ushort) code, reason) { }
internal void Stop(CloseStatusCode code, string reason) { Stop((ushort)code, reason); }
/// <summary> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/>, and /// releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating /// the reason for closure. /// </param> public void Close(CloseStatusCode code) { Close (code, null); }
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/>, and releases all associated resources. /// </summary> /// <remarks> /// This method doesn't wait for the close to be complete. /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> public void CloseAsync (CloseStatusCode code) { var msg = _readyState.CheckIfClosable (); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); return; } if (code.IsNoStatusCode ()) { closeAsync (new CloseEventArgs (), true, true); return; } var send = !code.IsReserved (); closeAsync (new CloseEventArgs (code), send, send); }
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>, and releases /// all associated resources. /// </summary> /// <remarks> /// <para> /// This method doesn't wait for the close to be complete. /// </para> /// <para> /// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> /// is greater than 123 bytes. /// </para> /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating /// the reason for closure. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for closure. /// </param> public void CloseAsync(CloseStatusCode code, string reason) { byte [] data = null; var msg = _readyState.CheckIfClosable () ?? (data = ((ushort) code).Append (reason)).CheckIfValidControlData ("reason"); if (msg != null) { _logger.Error (String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); error (msg); return; } var send = _readyState == WebSocketState.Open && !code.IsReserved (); closeAsync (new PayloadData (data), send, send); }
private static bool IsOpcodeReserved(CloseStatusCode code) => code == CloseStatusCode.Undefined || code == CloseStatusCode.NoStatus || code == CloseStatusCode.Abnormal || code == CloseStatusCode.TlsHandshakeFailure;
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>, and releases /// all associated resources. /// </summary> /// <remarks> /// <para> /// This method doesn't wait for the close to be complete. /// </para> /// <para> /// This method emits a <see cref="OnError"/> event if the size of /// <paramref name="reason"/> is greater than 123 bytes. /// </para> /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code indicating /// the reason for the close. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the close. /// </param> public void CloseAsync (CloseStatusCode code, string reason) { var msg = _readyState.CheckIfAvailable (true, true, false, false) ?? CheckCloseParameters (code, reason, _client); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); return; } if (code == CloseStatusCode.NoStatus) { closeAsync (new CloseEventArgs (), true, true); return; } var send = !code.IsReserved (); closeAsync (new CloseEventArgs (code, reason), send, send); }
private void Fatal(string message, CloseStatusCode code) => InternalCloseAsync(new PayloadData((ushort)code, message), !IsOpcodeReserved(code), false).Await();
private bool processUnsupportedFrame(WsFrame frame, CloseStatusCode code, string reason) { _logger.Debug ("Unsupported frame:\n" + frame.PrintToString (false)); processException (new WebSocketException (code, reason), null); return false; }
/// <summary> /// Closes the WebSocket connection with the specified <paramref name="code"/> and /// releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicates the status code for closure. /// </param> public void Close(CloseStatusCode code) { close((ushort)code, String.Empty); }
/// <summary> /// Closes the WebSocket connection with the specified <see cref="CloseStatusCode"/> and /// <see cref="string"/>, and releases all associated resources. /// </summary> /// <remarks> /// This method emits a <see cref="OnError"/> event if the length of <paramref name="reason"/> /// is greater than 123 bytes. /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicate the status codes for closure. /// </param> /// <param name="reason"> /// A <see cref="string"/> that contains the reason for closure. /// </param> public void Close(CloseStatusCode code, string reason) { var data = ((ushort) code).Append (reason); var msg = data.CheckIfValidCloseData (); if (msg != null) { _logger.Error (String.Format ("{0}\nreason: {1}", msg, reason)); error (msg); return; } var send = _readyState == WebSocketState.OPEN && !code.IsReserved (); close (new PayloadData (data), send, true); }
private void fatal (string message, CloseStatusCode code) { close (new CloseEventArgs (code, message), !code.IsReserved (), false, false); }
/// <summary> /// Closes the WebSocket connection with the specified <paramref name="code"/> and /// releases all associated resources. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> values that indicates the status code for closure. /// </param> public void Close(CloseStatusCode code) { close (new PayloadData (((ushort) code).ToByteArray (ByteOrder.BIG))); }