Skip to content

Wrapper over the WebSocket class to manage disconnections transparently

License

Notifications You must be signed in to change notification settings

AndreasSaltsberg/WebSocketWrapper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocketWrapper

Wrapper over the WebSocket class to manage disconnections transparently.

Installation

  • With npm:
$ npm install websocketwrapper

Usage in Node/Browserify

var WebSocketWrapper = require('websocketwrapper');

Usage Example

var ws = new WebSocketWrapper(url, protocol);

ws.reconnectionDelay = 5000;

ws.onopen = function() {
    console.log('connected');

    // Send some kind of registration message.
    ws.send('REGISTER');
};

ws.onreconnect = function() {
    console.log('reconnected');

    // Send some kind of registration message.
    ws.send('REGISTER');
};

ws.onmessage = function(e) {
    // do something
};

Documentation

WebSocketWrapper Class API

An instance of WebSocketWrapper holds an instance of a native WebSocket. The exposed API is the same as the native one. Just some differences/additions are described below.

new WebSocketWrapper(url, protocols) constructor

  • url and protocols mean the same as in the native API.

Events

open event is fired for the first WebSocket connection. If the connection is closed by the server or due to a netwotk error then close is fired and, after a delay, a new connection is attempted. If it connects to the server, reconnect event is fired.

Custom API

ws.reconnectionDelay property

Reconnection delay (in milliseconds). Default value is 5000 ms. If it's set to 0 no reconnection attempt will be performed.

ws.reconnectionDelay = 2000;

Debugging

The library includes the Node debug module and produces logs with prefix WebSocketWrapper.

Author

Iñaki Baz Castillo.

License

ISC.

About

Wrapper over the WebSocket class to manage disconnections transparently

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%