Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Open WebSocket connection
        /// </summary>
        public void Connect()
        {
            int ret = WebSocketConnect(this.instanceId);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Send string data over the socket.
        /// (JSLib implementation is converted to binary)
        /// </summary>
        /// <param name="data">Payload data.</param>
        public void Send(string data)
        {
            byte[] byteData = Encoding.UTF8.GetBytes(data);
            int    ret      = WebSocketSend(this.instanceId, byteData, byteData.Length);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
        public void Send(string str)
        {
            Debug.Log(str);
            int ret = WebSocketSendStr(this.instanceId, str);

            if (ret < 0)
            {
                throw WebSocketHelpers.GetErrorMessageFromCode(ret, null);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// WebSocket constructor.
        /// </summary>
        /// <param name="url">Valid WebSocket URL.</param>
        public WebSocket(string url)
        {
            try
            {
                // Create WebSocket instance
                this.ws = new WebSocketSharp.WebSocket(url);

                // Bind OnOpen event
                this.ws.OnOpen += (sender, ev) =>
                {
                    this.OnOpen?.Invoke();
                };

                // Bind OnMessage event
                this.ws.OnMessage += (sender, ev) =>
                {
                    if (ev.Data != null)
                    {
                        this.OnMessage?.Invoke(System.Text.Encoding.UTF8.GetBytes(ev.Data));
                    }
                };

                // Bind OnError event
                this.ws.OnError += (sender, ev) =>
                {
                    this.OnError?.Invoke(ev.Message);
                };

                // Bind OnClose event
                this.ws.OnClose += (sender, ev) =>
                {
                    this.OnClose?.Invoke(
                        WebSocketHelpers.ParseCloseCodeEnum((int)ev.Code)
                        );
                };
            }
            catch (Exception e)
            {
                throw new WebSocketUnexpectedException("Failed to create WebSocket Client.", e);
            }
        }
Ejemplo n.º 8
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));
 }
 /// <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)
 {
     Debug.Log("OnClose fired");
     _isAlive = false;
     this.OnClose?.Invoke(WebSocketHelpers.ParseCloseCodeEnum(closeCode));
 }