Last active
December 31, 2020 12:13
-
-
Save z-hao-wang/c1ef52279805133e1c9cd31262a58d45 to your computer and use it in GitHub Desktop.
auto reconnect websocket client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as WebSocket from 'ws'; | |
| import * as EventEmitter from 'events'; | |
| // credit to https://github.com/websockets/ws/wiki/Websocket-client-implementation-for-auto-reconnect | |
| class WebSocketClient extends EventEmitter{ | |
| private number = 0; // Message number | |
| private autoReconnectInterval = 5 * 1000; // ms | |
| private url: string = ''; | |
| private instance: WebSocket | null = null; | |
| open(url: string) { | |
| this.url = url; | |
| this.instance = new WebSocket(this.url); | |
| this.instance.on('open',()=>{ | |
| this.onopen(); | |
| }); | |
| this.instance.on('message',(data: string, flags: any)=>{ | |
| this.number++; | |
| this.onmessage(data, flags, this.number); | |
| }); | |
| this.instance.on('close',(e: any)=>{ | |
| switch (e.code){ | |
| case 1000: // CLOSE_NORMAL | |
| console.log("WebSocket: closed"); | |
| break; | |
| default: // Abnormal closure | |
| this.reconnect(e); | |
| break; | |
| } | |
| this.onclose(e); | |
| }); | |
| this.instance.on('error',(e: any)=>{ | |
| switch (e.code){ | |
| case 'ECONNREFUSED': | |
| this.reconnect(e); | |
| break; | |
| default: | |
| this.onerror(e); | |
| break; | |
| } | |
| }); | |
| } | |
| send (data: string, option?: any) { | |
| if (!this.instance) { | |
| return console.error('socket instance is not initialized. must call open(url) first'); | |
| } | |
| if (this.instance.readyState !== 1) { | |
| return console.error('socket instance is not in ready state', this.instance.readyState); | |
| } | |
| try{ | |
| this.instance.send(data, option); | |
| } catch (e){ | |
| this.instance.emit('error', e); | |
| } | |
| } | |
| reconnect(e: any) { | |
| console.log(`WebSocketClient: retry in ${this.autoReconnectInterval}ms`,e); | |
| this.instance && this.instance.removeAllListeners(); | |
| this.instance = null; | |
| setTimeout(() => { | |
| console.log(`WebSocketClient: reconnecting... in ${this.autoReconnectInterval}ms`); | |
| this.open(this.url); | |
| }, this.autoReconnectInterval); | |
| } | |
| onopen() { | |
| console.log("WebSocketClient: open", arguments); | |
| this.emit('open'); | |
| } | |
| onmessage(data: string, flag: any, _number: number) { | |
| this.emit('message', data, flag); | |
| } | |
| onerror(e: any) { | |
| console.log("WebSocketClient: errpr", arguments); | |
| this.emit('error', e); | |
| } | |
| onclose(e: any) { | |
| console.log("WebSocketClient: close", arguments); | |
| this.emit('close', e); | |
| } | |
| } | |
| export default WebSocketClient; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment