websocket 封装(含心跳,重连机制,强制退出)
const WS = function (json, url, callback, protocol) {
this.json = json;
this.url = url;
this.protocol = typeof (protocol) == 'undefined' ? null : protocol || ''
this.reconnect_time = 0;
this.reconnect_count = 0;
this.is_destroy = false
this.is_reconnect = false;
this.last_health_time = -1;
this.keep_alive_timer;
this.reconnect_timer;
this.socket;
this.initWs();
this.onopen = e => {
if (this.socket) {
this.socket.send(json);
this.reconnect_count = 0;
this.reconnect_time = 0;
if (this.socket.readyState === 1) {
this.keep_alive_timer = setInterval(()