2.0.4 • Published 4 years ago
reconnection v2.0.4
reconnection
features
- support browser and nodejs connection client
- support client of websocket, tcp and so on
install
npm i reconnection
usage
// nodejs:
import Reconnector from "reconnection";
import * as WebSocket from "ws";
// browser(module):
import Reconnector from "reconnection";
// browser(script tag):
// <script src="./node_modules/reconnection/reconnection.min.js"></script>
let ws;
const reconnector = new Reconnector(() => {
console.log(`connecting...`);
ws = new WebSocket("ws://localhost:8000");
ws.onclose = () => {
console.log(`disconnected...`);
reconnector.reconnect();
};
ws.onopen = () => {
console.log("connected...");
reconnector.reset();
};
ws.onerror = error => {
console.log(error);
};
});
options
const reconnector = new Reconnector(() => {
// ...
}, {
startTimeout: 3000, // 3000->4500->6750->10125->15187->22781->30000->30000...
increaseRate: 1.5,
endTimeout: 30000,
maxTimes: Infinity // for Infinity, never give up; for 3, just reconnect 3 times, if fails, stop.
});
change logs
// v2
import Reconnector from "reconnection/nodejs/nodejs";
import Reconnector from "reconnection/browser/browser";
// v1
import { Reconnector } from "reconnection/nodejs";
import { Reconnector } from "reconnection/browser";