websocket-client icon indicating copy to clipboard operation
websocket-client copied to clipboard

Handle 401 Unauthorized response when connecting

Open nicktgn opened this issue 2 years ago • 1 comments

I there a way to intercept and handle any specific server responses like 401 Unauthorized when connecting to websocket using ws.Start() or ws.StartOrFail().

I was trying to use ws.StartOrFail() but it just throws some generic error Unable to connect to the remote server that doesn't have any information about the reason for the error.

Thanks. Would appreciate any help.

nicktgn avatar Jul 09 '23 09:07 nicktgn

Hey, sorry for the late response. The original exception is wrapped by our WebsocketException. So check ex.InnerException, it should give you what you need. This stands for ws.StartOrFail().

If you want to use the background method ws.Start(), you can follow something like this:

ws.DisconnectionHappened.Subscribe(info => {
    if(info.Exception is MyException myEx && myEx.Code == "401") {
        // stop reconnection
        info.CancelReconnection = true;
    }                    
});

Marfusios avatar Aug 05 '23 16:08 Marfusios