Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure WebSocket fired all Closed events when Disposing #162

Open
darxis opened this issue Oct 4, 2019 · 0 comments
Open

Make sure WebSocket fired all Closed events when Disposing #162

darxis opened this issue Oct 4, 2019 · 0 comments
Labels

Comments

@darxis
Copy link

darxis commented Oct 4, 2019

In my app I need to dispose the WebSocket but if the WebSocket is connected then I would like to make sure that the Closed event will get fired. If I call the WebSocket.Dispose() when the WS is connected then it will close the connection but the Closed event will not get fired. How can I easily make sure that it will get fired? Do I need to implement custom logic for that?

Here is my wrapper for WebSocket:

internal class WebSocket4NetWrapper : IWebSocketWrapper
    {
        private bool _disposed;

        private readonly WebSocket _ws;

        public event EventHandler Connected;

        public event EventHandler<WebSocketClientMessageReceivedEventArgs> MessageReceived;

        public event EventHandler<WebSocketClientDataReceivedEventArgs> DataReceived;

        public event EventHandler Disconnected;

        public WebSocketState State
        {
            get
            {
                var state = _ws.State;
                switch (state)
                {
                    case WebSocket4NetState.None:
                        return WebSocketState.None;
                    case WebSocket4NetState.Connecting:
                        return WebSocketState.Connecting;
                    case WebSocket4NetState.Open:
                        return WebSocketState.Open;
                    case WebSocket4NetState.Closing:
                        return WebSocketState.Closing;
                    case WebSocket4NetState.Closed:
                        return WebSocketState.Closed;
                    default:
                        throw UnhandledEnumValueException.Create(state);
                }
            }
        }

        public WebSocket4NetWrapper(WebSocket webSocket)
        {
            _ws = webSocket;
            _ws.Opened += WebSocketOnOpen;
            _ws.Closed += WebSocketOnClose;
            _ws.MessageReceived += WebSocketOnMessageReceived;
            _ws.DataReceived += WebSocketOnDataReceived;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _ws.Dispose();
            }

            _disposed = true;
        }

        public void Connect()
        {
            _ws.Open();
        }

        public void Disconnect()
        {
            _ws.Close();
        }

        public void SendMessage(string message)
        {
            _ws.Send(message);
        }

        public void SendMessage(byte[] message)
        {
            _ws.Send(message, 0, message.Length);
        }

        private void WebSocketOnOpen(object sender, EventArgs e)
        {
            Connected?.Invoke(this, EventArgs.Empty);
        }

        private void WebSocketOnClose(object sender, EventArgs e)
        {
            Disconnected?.Invoke(this, EventArgs.Empty);
        }

        private void WebSocketOnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            MessageReceived?.Invoke(this, new WebSocketClientMessageReceivedEventArgs(e.Message));
        }

        private void WebSocketOnDataReceived(object sender, DataReceivedEventArgs e)
        {
            DataReceived?.Invoke(this, new WebSocketClientDataReceivedEventArgs(e.Data));
        }
    }
@kerryjiang kerryjiang added the bug label Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants