npm

ws-reconnect

1.0.7 • Public • Published

ws-reconnect: a Node.js WebSocket Client

npm version

Usage

Server Example

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 1010 });


wss.on('connection', function connection(ws) {
  console.log("new connection");
  ws.on('message', function incoming(data) {
    console.log("incoming message: "+data);
  });
  
  setInterval(function(){
      ws.close();
  },4000);
  setInterval(function(){
      if(ws.readyState === 1)
        ws.send("hello");
  },1000);
});

Client Example

var WSCLINET = require('ws-reconnect');
var wsclient = new WSCLIENT("localhost:1010",{
    retryCount:1, // default is 2
    reconnectInterval: 1 // default is 5
});
wsclient.start();
wsclient.on("message",function(data){
    console.log(data);
});

wsclient.on("reconnect",function(){
    console.log("reconnecting");
});
wsclient.on("connect",function(){
    console.log("connected");
});
wsclient.on("destroyed",function(){
    console.log("destroyed");
});

Package Sidebar

Install

npm i ws-reconnect

Weekly Downloads

26

Version

1.0.7

License

ISC

Unpacked Size

40.6 kB

Total Files

6

Last publish

Collaborators

  • ofirattia