pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.22k stars 197 forks source link

Preserve options from `@types/ws` - missing origin #80

Closed DenisCarriere closed 5 years ago

DenisCarriere commented 6 years ago

Would it be possible to add the same options as @types/ws, that way it's a simple "drop-in" for ws with the extra features of reconnecting.

import WebSocket from 'reconnecting-websocket';

const origin = "https://example.com";
const ws = new WebSocket("wss://<SERVER>", [], {origin});

Client Option types from @types/ws

    interface ClientOptions {
        protocol?: string;
        handshakeTimeout?: number;
        perMessageDeflate?: boolean | PerMessageDeflateOptions;
        localAddress?: string;
        protocolVersion?: number;
        headers?: { [key: string]: string };
        origin?: string;
        agent?: http.Agent;
        host?: string;
        family?: number;
        checkServerIdentity?(servername: string, cert: CertMeta): boolean;
        rejectUnauthorized?: boolean;
        passphrase?: string;
        ciphers?: string;
        cert?: CertMeta;
        key?: CertMeta;
        pfx?: string | Buffer;
        ca?: CertMeta;
        maxPayload?: number;
    }
pladaria commented 6 years ago

This library is meant to be a "drop-in" replacement for WebSocket libraries that implement the WebSocket API.

reconnecting-websocket tries to be compatible with WebSockets from browser, React Native and any API compatible library. Many of the client options defined for ws are not aplicable.

If you want to use full ws capabilities, you can, simply create a new Class extending it:

import WS from `ws`;
import RWS from `reconnecting-websocket`;

...

class MyWebSocket extends WS {
    constructor(url, protocol) {
        super(/*pass here your ws options*/)
    }
}

// Then you can use reconnecting-websocket

const rws = new RWS("url", [], {WebSocket: MyWebSocket, etc});