pladaria / reconnecting-websocket

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

No messages received after reconnect. #59

Closed gravesjohnr closed 6 years ago

gravesjohnr commented 6 years ago

It is very odd. The reconnecting-websocket works very well showing a reconnect after a server is restarted. I also see data being sent thought this websocket, but for some odd reason, after a reconnect, my on message function is no longer called. It is very simple code. Any ideas what I'm doing wrong? I get messages the first time the code is run, but after a reconnect, the "On Message" is never executed.

const WebSocket = require('ws');
const ReconnectingWebSocket = require('reconnecting-websocket');
var socketUrl = instance.options.url;
instance.debug("WebSocket: Connect to: "+socketUrl);
instance.websocket = new ReconnectingWebSocket(socketUrl,[],{ constructor: WebSocket, debug: true });
instance.websocket.on('message', function(data) {
  console.log("On Message");
});
instance.websocket.on('close', function close() {
  console.log('disconnected');
});
pladaria commented 6 years ago

Hi, this is a known issue when using ws as WebSocket constructor. This will get fixed on the next release of reconnecting-websocket.

Meanwhile, you can use html5-websocket as shown in the README examples.

Sorry for the inconveniences

gravesjohnr commented 6 years ago

Thanks. That did it. Same code, but using html5-websocket.

pladaria commented 6 years ago

I'v just published a new version which is a complete rewrite and should be compatible with ws. Give it a try. Thanks!