dimaaan / WebSocketTextClient

Wrapper around System.Net.WebSockets.ClientWebSocket that provides event based interface to exchange text messages over Web Sockets. Support cancellation
MIT License
1 stars 1 forks source link

Add additional events and better event args #1

Closed Ruhrpottpatriot closed 7 years ago

Ruhrpottpatriot commented 7 years ago

This pull request added additional events for errors, open and close. These events and the OnMessage event now use the EventArgs or EventArgs<T> type to pass along information to the subscriber. This allows the user to manage multiple socket connections and differentiate by sender.

I also changed all event handler invocations (except Close) to be asynchronous, which was done with the Task.Factory.FromAsync() method.

Last I added a method to allow the user to add his own headers to the initial socket request. This allows greater flexibility with things such as authorization,

Ruhrpottpatriot commented 7 years ago

Btw: I intended this as a starting point. There's still more room for improvement. For example, I find it a bit counter intuitive, how you handled the keep alive process. If I understood it correctly, you call OpenAnsyc() and that Task only completes when the connection is closed.

Maybe change the code, so that every public async method accepts a CancellationToken and the receive Task is decoupled from ConnectAsync().

TaskCompletionSource could be an option. I'll look into it.

dimaaan commented 7 years ago

I intended this as a starting point. There's still more room for improvement...

Currently, cancel a task via token is the only way to close connection. Agree that this should be improved. I'll create a separate issue for this