pladaria / reconnecting-websocket

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

error in use #34

Closed pengdou closed 7 years ago

pengdou commented 7 years ago

i use 'reconnecting-websocket' in my project.but there is a error that i can't fix it. the error occured on using module 'reconnecting-websocket',but it dismissed while use 'ws'. sorry for poor English,i'm chinese.

var ReconnectingWebSocket = require('reconnecting-websocket'); var options = {constructor:WebSocket,debug: true, reconnectInterval: 3000}; var regWs = new ReconnectingWebSocket(config.server,[], options); // var regWs = new WebSocket("ws://127.0.0.1:60001"); regWs.on('open', function() { regNode(regWs); }); regWs.on('error', function(e) { console.log('error occured:'+e); }); regWs.on('close', function() { console.log('closed'); }); regWs.on('message', function(message) { var msg = {}; try{ msg = JSON.parse(message); }catch(e){ console.log('unresolved msg:'+message); } processMessage(msg,regWs); });

RWS: init RWS: connect RWS: bypass properties error occured:Error: connect ECONNREFUSED 127.0.0.1:60001 RWS: handleClose { shouldRetry: true } RWS: retries count: 1 RWS: handleClose - reconnectDelay: 1620.2790890714393 closed events.js:163 throw er; // Unhandled 'error' event ^

Error: connect ECONNREFUSED 127.0.0.1:60001 at Object.exports._errnoException (util.js:1050:11) at exports._exceptionWithHostPort (util.js:1073:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14) RWS: connect RWS: bypass properties

pengdou commented 7 years ago

and another problem if i don't start the server and run the client , i can see "RWS: timeout" print two times in console,then the client exit.how can i let it try reconnect forever?

var WebSocket = require('ws'); var ReconnectingWebSocket = require('reconnecting-websocket'); var options = {constructor:WebSocket,debug: true}; var regWs = new ReconnectingWebSocket('ws://127.0.0.1:60001',[], options); // var regWs = new WebSocket("ws://127.0.0.1:60001"); regWs.on('open', function() { regNode(regWs); }); regWs.on('error', function(e) { console.log('error occured:'+e); }); regWs.on('close', function() { console.log('closed'); }); regWs.on('message', function(message) { var msg = {}; try{ msg = JSON.parse(message); }catch(e){ console.log('unresolved msg:'+message); } processMessage(msg,regWs); });

/usr/local/bin/node /linData/work/vixtel/speed_test/testnode/test.js RWS: init RWS: connect RWS: bypass properties error occured:Error: connect ECONNREFUSED 127.0.0.1:60001 error occured:Error: connect ECONNREFUSED 127.0.0.1:60001 RWS: handleClose { shouldRetry: true } RWS: retries count: 1 RWS: handleClose - reconnectDelay: 2957.833110837981 closed RWS: connect RWS: bypass properties error occured:Error: connect ECONNREFUSED 127.0.0.1:60001 RWS: timeout RWS: timeout

Process finished with exit code 0

pladaria commented 7 years ago

You are using the wrong API. Please, read the README first.

This module has the browser's websocket interface.

You can't set event listeners using on('something', callback), instead you must use:

ws.addEventListener('event-name', callback') or ws.onEventName = callback;

You can see some examples in the README