示例#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);
            }
        }