jchristn / WatsonWebsocket

A simple C# async websocket server and client for reliable transmission and receipt of data
MIT License
277 stars 53 forks source link

How to disconnect a client from the server? #128

Closed alec1o closed 1 year ago

alec1o commented 1 year ago

I wanna disconnect a client, sample code below.

wss.ClientConnected += (_, input) =>
{            
    string token = input.HttpRequest.Headers.Get("token");
    if (string.IsNullOrWhiteSpace(token))
    {
        // token not found, todo disconnect user          
    }

    if (MyClass.VerifyToken(token) is false)
    {
        // invalid token, todo disconnect client
    }

    // .. my logic here
};
jchristn commented 1 year ago

The WatsonWsServer has a DisconnectClient method that provides this function.

alec1o commented 1 year ago

Thanks!

wss.ClientConnected += (_, input) =>
{            
    string token = input.HttpRequest.Headers.Get("token");
    if (string.IsNullOrWhiteSpace(token))
    {
        // disconnecting client
        wss.DisconnectClient(input.Client.Guid);
        return;
    }

    if (MyClass.VerifyToken(token) is false)
    {
        // disconnecting client
        wss.DisconnectClient(input.Client.Guid);
        return;
    }

    // .. my logic here
};