コード例 #1
0
     internal WebSocketException(
   CloseStatusCode code, string message, Exception innerException
 )
         : base(message ?? code.GetMessage (), innerException)
     {
         _code = code;
     }
コード例 #2
0
ファイル: Socket.cs プロジェクト: janevin/CSharpPhoenixClient
        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();
        }
コード例 #3
0
 /// <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);
 }
コード例 #4
0
 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;
 }
コード例 #5
0
 /// <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);
 }
コード例 #6
0
 internal WebSocketException (CloseStatusCode code)
   : this (code, null, null)
 {
 }
コード例 #7
0
 public static WsFrame CreateCloseFrame(Mask mask, CloseStatusCode code, string reason)
 {
     return(new WsFrame(Opcode.Close, mask, new PayloadData(((ushort)code).Append(reason))));
 }
コード例 #8
0
 internal WebSocketException (CloseStatusCode code, Exception innerException)
   : this (code, null, innerException)
 {
 }
コード例 #9
0
 protected Task CloseAsync(CloseStatusCode statusCode, string reason)
 {
     return(WebSocket.CloseAsync((int)statusCode, reason));
 }
コード例 #10
0
 protected virtual Task OnDisconnected(CloseStatusCode statusCode, string reason)
 {
     return(Task.CompletedTask);
 }
コード例 #11
0
        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
        }
コード例 #12
0
 internal void Close(CloseStatusCode code = CloseStatusCode.Normal, string reason = null)
 {
     Context.WebSocket.Close(code, reason);
 }
コード例 #13
0
ファイル: WebSocketService.cs プロジェクト: KqSMea8/HS.Admin
 public void SJd(CloseStatusCode code, string reason)
 {
     SJd((ushort)code, reason);
 }
コード例 #14
0
 protected override Task OnDisconnected(CloseStatusCode code, string reason)
 {
     Console.WriteLine($"{GetType().Name} Disconnected with code: " + code.ToString());
     return(base.OnDisconnected(code, reason));
 }
コード例 #15
0
 /// <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);
 }
コード例 #16
0
 public CloseEventArgs(CloseStatusCode code)
     : this((ushort)code, null)
 {
 }
コード例 #17
0
 internal CloseEventArgs(CloseStatusCode code)
     : this((ushort)code, null, false)
 {
 }
コード例 #18
0
 /// <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);
 }
コード例 #19
0
 internal WebSocketException(CloseStatusCode code)
     : this(code, code.GetMessage())
 {
 }
コード例 #20
0
 private void close(CloseStatusCode code, string reason)
 {
     close((ushort)code, reason);
 }
コード例 #21
0
 internal WebSocketException(CloseStatusCode code, string reason)
     : this(code, reason, null)
 {
 }
コード例 #22
0
        /// <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);
        }
コード例 #23
0
 internal CloseEventArgs (CloseStatusCode code)
   : this ((ushort) code)
 {
 }
コード例 #24
0
        private bool processUnsupportedFrame(WebSocketFrame frame, CloseStatusCode code, string reason)
        {
            processException(new WebSocketException(code, reason), null);

            return false;
        }
コード例 #25
0
ファイル: WebSocket.cs プロジェクト: jijamw/websocket-sharp
 /// <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);
 }
コード例 #26
0
 internal WebSocketException(CloseStatusCode code, string reason)
     : this(code, reason, null)
 {
 }
コード例 #27
0
 private void close(CloseStatusCode code, string reason, bool wait)
 {
     close (new PayloadData (((ushort) code).Append (reason)), !code.IsReserved (), wait);
 }
コード例 #28
0
 /// <inheritdoc />
 public Task CloseAsync(CloseStatusCode code, string?comment = null, CancellationToken cancellationToken = default)
 => UnderlyingWebSocket.CloseAsync(MapCloseStatus(code), comment ?? string.Empty, cancellationToken);
コード例 #29
0
 /// <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);
 }
コード例 #30
0
 private WebSocketCloseStatus MapCloseStatus(CloseStatusCode code) => code switch
 {
コード例 #31
0
ファイル: WebSocket.cs プロジェクト: NIAEFEUP/Kommando
 /// <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);
 }
コード例 #32
0
 internal WebSocketException(CloseStatusCode code) : this(code, null, null)
 {
 }
コード例 #33
0
 public CloseEventArgs(CloseStatusCode code, string reason)
     : this((ushort)code, reason)
 {
 }
コード例 #34
0
 internal WebSocketException(CloseStatusCode code, Exception innerException) : this(code, null, innerException)
 {
 }
コード例 #35
0
 internal CloseEventArgs(CloseStatusCode code, string reason, bool wasClean)
     : this((ushort)code, reason, wasClean)
 {
 }
コード例 #36
0
 internal WebSocketException(CloseStatusCode code, string message) : this(code, message, null)
 {
 }
コード例 #37
0
 protected override Task OnDisconnected(CloseStatusCode statusCode, string reason)
 {
     DisconnectedCalled = true;
     return(base.OnDisconnected(statusCode, reason));
 }
コード例 #38
0
 internal WebSocketException(CloseStatusCode code, string message, Exception innerException) : base(
         message ?? code.GetMessage(), innerException)
 {
     Code = code;
 }
コード例 #39
0
 internal WebSocketException(CloseStatusCode code, string message)
     : base(message)
 {
     Code = code;
 }
コード例 #40
0
 private void close(CloseStatusCode code, string reason, bool wait)
 {
     close(new PayloadData(((ushort)code).Append(reason)), !code.IsReserved(), wait);
 }
コード例 #41
0
 /// <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);
 }
コード例 #42
0
        private bool processUnsupportedFrame(WebSocketFrame frame, CloseStatusCode code, string reason)
        {
            processException(new WebSocketException(code, reason), null);

            return(false);
        }
コード例 #43
0
   internal WebSocketException(
 CloseStatusCode code, string reason, Exception innerException)
       : base(reason ?? code.GetMessage (), innerException)
   {
       Code = code;
   }
コード例 #44
0
ファイル: WebSocket.cs プロジェクト: khinbaptista/OBS-tray
        /// <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);
        }
コード例 #45
0
 internal WebSocketException (CloseStatusCode code, string message)
   : this (code, message, null)
 {
 }
コード例 #46
0
ファイル: WebSocket.cs プロジェクト: khinbaptista/OBS-tray
        /// <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);
        }
コード例 #47
0
 internal CloseEventArgs (CloseStatusCode code, string reason)
   : this ((ushort) code, reason)
 {
 }
コード例 #48
0
 internal void Stop(CloseStatusCode code, string reason)
 {
     Stop((ushort)code, reason);
 }
コード例 #49
0
ファイル: WebSocket.cs プロジェクト: jijamw/websocket-sharp
 /// <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);
 }
コード例 #50
0
ファイル: WebSocket.cs プロジェクト: rjansen/unity-ws-demo
    /// <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);
    }
コード例 #51
0
ファイル: WebSocket.cs プロジェクト: jijamw/websocket-sharp
        /// <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);
        }
コード例 #52
0
ファイル: WebSocket.cs プロジェクト: kpspence/embedio
 private static bool IsOpcodeReserved(CloseStatusCode code)
 => code == CloseStatusCode.Undefined ||
 code == CloseStatusCode.NoStatus ||
 code == CloseStatusCode.Abnormal ||
 code == CloseStatusCode.TlsHandshakeFailure;
コード例 #53
0
    /// <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);
    }
コード例 #54
0
ファイル: WebSocket.cs プロジェクト: kpspence/embedio
 private void Fatal(string message, CloseStatusCode code)
 => InternalCloseAsync(new PayloadData((ushort)code, message), !IsOpcodeReserved(code), false).Await();
コード例 #55
0
        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;
        }
コード例 #56
0
ファイル: WebSocket.cs プロジェクト: johlo/websocket-sharp
 /// <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);
 }
コード例 #57
0
        /// <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);
        }
コード例 #58
0
 private void fatal (string message, CloseStatusCode code)
 {
   close (new CloseEventArgs (code, message), !code.IsReserved (), false, false);
 }
コード例 #59
0
 internal void Stop(CloseStatusCode code, string reason)
 {
     Stop((ushort)code, reason);
 }
コード例 #60
0
ファイル: WebSocket.cs プロジェクト: NIAEFEUP/Kommando
 /// <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)));
 }