Marfusios / websocket-client

🔧 .NET/C# websocket client library
MIT License
682 stars 126 forks source link

New feature : generate events optionally #136

Open frogeater opened 1 year ago

frogeater commented 1 year ago

Thank you very much for this websocket client which has made my life so much easier. I am currently optimizing event-driven powershell scripts that collect and consolidate information from many sources. Some of them are websocket servers. To be able to generate events easily usable by Powershell I created a C# class derived from WebsocketClient inserted into the scripts. In my opinion, having the three events and the SubscribeEvents method available directly in the WebsocketClient class would be a welcome addition to event-driven applications. The SubscribeEvents method is available to users, who may or may not use it as required. Here's the class I use.

public class WebsocketClientExt : WebsocketClient {

    public event EventHandler<ReconnectionInfo> Connected;
    public event EventHandler<ResponseMessage> Received;
    public event EventHandler<DisconnectionInfo> Disconnected;

    public WebsocketClientExt( Uri url ): base(url) { }

    public void SubscribeEvents () {
        ReconnectionHappened.Subscribe(c => Connected?.Invoke(this, c));
        MessageReceived.Subscribe(m => Received?.Invoke(this, m));
        DisconnectionHappened.Subscribe(d => Disconnected?.Invoke(this, d));
    }

}

Alain