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

add options for websocket

Open Zerounary opened this issue 3 years ago • 2 comments

add options for websocket

Zerounary avatar Feb 16 '22 01:02 Zerounary

I'd love this PR to be merged since I need the rejectUnauthorized option to support self-signed certificates

craftycram avatar Mar 21 '23 02:03 craftycram

For anyone waiting for this PR, here's a decent workaround:

import ReconnectingWebSocket from 'reconnecting-websocket';
import { type Options, type UrlProvider } from 'reconnecting-websocket';
import WebSocket, { type ClientOptions } from 'ws';

const wsFactory = (options: ClientOptions) =>
  class WsOptions extends WebSocket {
    constructor(url: string, protocols?: string | string[]) {
      super(url, protocols, options);
    }
  };

export class KeepAliveWs extends ReconnectingWebSocket {
  constructor(
    url: UrlProvider,
    protocols?: string | string[],
    options: Options = {},
  ) {
    const { connectionTimeout = 4000 } = options;
    const opts = {
      ...options,
      connectionTimeout: connectionTimeout + 1000, // give WS time to disconnect without throwing
      WebSocket: wsFactory({ handshakeTimeout: connectionTimeout }), // PASS CUSTOM ARGUMENTS HERE
    };

    super(url, protocols, opts);
  }
}

barlock avatar May 22 '23 13:05 barlock