https://github.com/kocisov/adventure
Simple WebSocket Client
https://github.com/kocisov/adventure
adventure client reconnect simple websocket
Last synced: 7 months ago
JSON representation
Simple WebSocket Client
- Host: GitHub
- URL: https://github.com/kocisov/adventure
- Owner: kocisov
- License: mit
- Created: 2017-05-22T20:31:49.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-28T14:04:50.000Z (about 9 years ago)
- Last Synced: 2025-02-12T08:16:34.647Z (over 1 year ago)
- Topics: adventure, client, reconnect, simple, websocket
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 11
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Adventure [](http://npmjs.com/package/@braind/adventure) [](https://codeclimate.com/github/braind/adventure) []()
> Simple WebSocket Client
## Features
- Reconnect implemented
- Handle Errors and Messages + Redux with your simple functions
## Installation
```bash
# yarn
yarn add @braind/adventure
# npm
npm install @braind/adventure --save
```
## Usage
```js
import adventureClient from '@braind/adventure';
import store from './redux/store';
const adventure = new adventureClient({
debug: true,
handleMessage,
maxReconnectAttempts: 5,
reconnect: true,
reconnectInterval: 5000,
reduxDispatcher,
responseType: 'json',
url: 'ws://localhost:3000'
});
function handleMessage(message) {
console.log(message);
if (message === 'Ping' || message.type === 'Ping') {
adventure.send('Pong');
}
}
function reduxDispatcher(message) {
const { type, data: payload } = message;
switch(type) {
case 'RECEIVED_INFO':
store.dispatch({
type: 'RECEIVED_INFO',
payload
});
default:
console.log('Received unspecified action type');
}
}
const currentSocketNumber = adventure.socketNumber();
const nextSocketNumber = adventure.nextSocketNumber();
const lastSocketNumber = adventure.lastSocketNumber();
```
## API
#### adventureClient([opts])
Creates Adventure Client
##### Options
- debug: boolean - default is false,
- handleError: function - optional,
- handleMessage: function - optional,
- maxReconnectAttempts: number - default is 3,
- reconnect: boolean - default is false,
- reconnectInterval: number - default is 5000ms,
- reduxDispatcher: function - optional,
- responseType: [json, text] - default is json,
- url: string -> 'ws://localhost:3000' - required