elws
Simple auto-reconnect WebSocket adapter
Install
npm i elws -S
Usage
const RWS = ; const client = 'ws://echo.websocket.org;ws://localhost:8080'; client { // as soon as we are connected // just send an Hello World! to the server client;}; client { // because the server is echo.websocket.org we should receive // "Hello World!" right after connection console;}; // connectclient; //close;
If
ws://echo.websocket.org
is unreachable, RWebSocket will try to reconnect once every 3 seconds (default) and try 3 times, then switch another url
See example.js for detailed usage.
API
The only modifications to the API are:
- the ability to give a 3rd argument to the constructor to set the
retryInterval
inms
- the ability to give a 4rth argument to the constructor to set the
retryCount
- the
#connect()
method to actually create a WebSocket and connect to the server
const client = 'ws://echo.websocket.org;ws://localhost:8080' null 250005;
Reconnection attempts will be made once every 25 seconds, and try 5 times then switch another url. NB: the 'null' param is for the protocol because the constructor is the same as the WebSocket RFC +
retryInterval
the ability to give a one argument to the constructor to set the close event 'code'
client;