pladaria / reconnecting-websocket

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

Typescript defs for close are missing third argument #44

Closed bvandenbos closed 6 years ago

bvandenbos commented 6 years ago

ReconnectingWebsocket extends WebSocket but doesn't redefine the close method to take the third "options" parameter.

Closing a reconnecting websocket like this:

rws.close(1000, '', opts);

Results in a typescript error; Expected 0-2 arguments, but got 3.

bennycode commented 6 years ago

I got the same issue with reconnecting-websocket v3.2.2:

unbenannt

Here is my code:

const Html5WebSocket = require('html5-websocket');
const ReconnectingWebsocket = require('reconnecting-websocket');

private socket: WebSocket;

// ...

public connect(clientId?: string): void { 
  this.socket = new ReconnectingWebsocket(...);
}

public disconnect(): void {
  // TS2554: Expected 0-2 arguments, but got 3.
  this.socket.close(1000, '', {keepClosed: true, fastClose: true, delay: 0});
}

Luckily there is a workaround:

public disconnect(): void {
  const socket: any = this.socket; // Cast 'WebSocket' into 'any' in order to supply a third parameter
  this.socket.close(1000, '', {keepClosed: true, fastClose: true, delay: 0});
}

Source: https://github.com/wireapp/wire-web-packages/pull/114

bennycode commented 6 years ago

Any update on this?

pladaria commented 6 years ago

Should be fixed in latest release. Thanks

bennycode commented 6 years ago

@pladaria Can you provide an example on how to use this library with TypeScript? I tried to use it but it has some problems.

Here is my code:

import ReconnectingWebsocket = require("reconnecting-websocket");

const socket = new ReconnectingWebsocket(
  () => this.buildWebSocketURL(),
  undefined,
  WebSocketClient.RECONNECTING_OPTIONS
);

It thows the following error:

image

pladaria commented 6 years ago

Hi,

Thanks for checking the new library version!

import ReconnectingWebsocket from 'reconnecting-websocket';
import WebSocket from 'ws';

const getUrl = () => Promise.resolve('ws://echo.websocket.org/');
const OPTIONS = {WebSocket};

const rws = new ReconnectingWebsocket(getUrl, undefined, OPTIONS);

rws.addEventListener('open', () => {
    rws.send('hi');
});

rws.addEventListener('message', (event) => {
    console.log(event.data); // prints "hi"
});

Please don't hesitate to open an issue if you find any problem

bennycode commented 5 years ago

@pladaria I checked the "close" function of v4.2.0 and it is defined to accept only 2 parameters ("code" and "reason"), so I guess there is no more support for a third ("options") parameter?