Пример #1
0
        /// <summary>
        /// Return WebSocket connection state.
        /// </summary>
        /// <returns>The state.</returns>
        public WebSocketState GetState()
        {
            int state = WebSocketGetState(this.instanceId);

            if (state < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(state, null);
            }

            switch (state)
            {
            case 0:
                return(WebSocketState.Connecting);

            case 1:
                return(WebSocketState.Open);

            case 2:
                return(WebSocketState.Closing);

            case 3:
                return(WebSocketState.Closed);

            default:
                return(WebSocketState.Closed);
            }
        }
Пример #2
0
        /// <summary>
        /// Close WebSocket connection with optional status code and reason.
        /// </summary>
        /// <param name="code">Close status code.</param>
        /// <param name="reason">Reason string.</param>
        public void Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null)
        {
            int ret = WebSocketClose(this.instanceId, (int)code, reason);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
Пример #3
0
        /// <summary>
        /// Send binary data over the socket.
        /// </summary>
        /// <param name="data">Payload data.</param>
        public void Send(byte[] data)
        {
            int ret = WebSocketSend(this.instanceId, data, data.Length);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
Пример #4
0
        /// <summary>
        /// Open WebSocket connection
        /// </summary>
        public void Connect()
        {
            int ret = WebSocketConnect(this.instanceId);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
Пример #5
0
 /// <summary>
 /// Delegate onClose event from JSLIB to native sharp event
 /// Is called by WebSocketFactory
 /// </summary>
 /// <param name="closeCode">Close status code.</param>
 public void DelegateOnCloseEvent(int closeCode)
 {
     this.OnClose?.Invoke(WebSocketHelpers.ParseCloseCodeEnum(closeCode), "");
 }