Simple WebSocket Client
- Reconnect implemented
- Handle Errors and Messages + Redux with your simple functions
# yarn
yarn add @braind/adventure
# npm
npm install @braind/adventure --save
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();
Creates Adventure Client
- 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