websocket-client
websocket-client copied to clipboard
Reconnection switch url as I needed to pass in access token (JWT) as url query string.
Hi, is there any way to pass in JWT token as url query string parameter during reconnection happen? As time to time the client might have disconnected and JWT token will be expired, so I need to pass in a new token when reconnection happen.
I have the exact same scenario: The 3rd-party system I connect to mandates that I include a token on the querystring and this token expires at regular intervals. Therefore, I must obtain a new token and change the URL from time to time. Fortunately, I can achieve this with the websocket-client client factory like so:
var clientFactory = new Func<Uri, CancellationToken, Task<WebSocket>>(async (uri, cancellationToken) =>
{
// The current value in the uri parameter must be ignored because it contains "access_token" which may or may not be expired.
// The following line ensures the "access_token" is refreshed whenever it expires.
uri = new Uri($"wss://ws.zoom.us/ws?subscriptionId={_subscriptionId}&access_token={_tokenHandler.Token}");
var client = new ClientWebSocket()
{
Options =
{
KeepAliveInterval = TimeSpan.Zero, // Turn off built-in "Keep Alive" feature because Zoom uses proprietary "heartbeat" every 30 seconds rather than standard "pong" messages at regular interval.
}
};
client.Options.SetRequestHeader("ZoomNet-Version", ZoomClient.Version);
await client.ConnectAsync(uri, cancellationToken).ConfigureAwait(false);
return client;
});
// Please note that the url specified on the following line will be ignored and will be replaced with the URI calculated in the client factory
var myWebsocketClient = new WebsocketClient(new Uri("wss://ws.zoom.us"), clientFactory);
Hope this helps
Woah! Thanks for the help! It works well :D