/// <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);
            }
        }
Beispiel #2
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));
 }