Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maxleiko/rwebsocket
Simple auto-reconnect WebSocket adapter
https://github.com/maxleiko/rwebsocket
Last synced: 8 days ago
JSON representation
Simple auto-reconnect WebSocket adapter
- Host: GitHub
- URL: https://github.com/maxleiko/rwebsocket
- Owner: maxleiko
- License: mit
- Created: 2016-12-16T14:51:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-08T16:42:09.000Z (almost 8 years ago)
- Last Synced: 2024-10-08T20:56:33.296Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rwebsocket [![Build Status](https://travis-ci.org/maxleiko/rwebsocket.svg?branch=master)](https://travis-ci.org/maxleiko/rwebsocket)
Simple auto-reconnect WebSocket adapter### Install
```sh
npm i rwebsocket -S
```
or
```sh
yarn add rwebsocket
```### Usage
```js
const RWebSocket = require('rwebsocket');const client = new RWebSocket('ws://echo.websocket.org');
client.onopen = function () {
// as soon as we are connected
// just send an Hello World! to the server
client.send('Hello World!');
};client.onmessage = function (event) {
// because the server is echo.websocket.org we should receive
// "Hello World!" right after connection
console.log('> ', event.data);
};// connect
client.connect();
```> If `ws://echo.websocket.org` is unreachable, RWebSocket will try to reconnect once every 3 seconds (default)
### API
The only modifications to the API are:
- the ability to give a 3rd argument to the constructor to set the `retryInterval` in `ms`
- the `#connect()` method to actually create a WebSocket and connect to the server```js
const client = new RWebSocket('ws://echo.websocket.org', null, 25000);
```
> Reconnection attempts will be made once every 25 seconds
> NB: the 'null' param is for the protocol because the constructor is the same as the [WebSocket RFC](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) + `retryInterval`