|
@@ -14,6 +14,7 @@
|
|
|
|
|
|
function WebsocketHeartbeat({
|
|
function WebsocketHeartbeat({
|
|
url,
|
|
url,
|
|
|
|
+ userId,
|
|
pingTimeout = 15000,
|
|
pingTimeout = 15000,
|
|
pongTimeout = 10000,
|
|
pongTimeout = 10000,
|
|
reconnectTimeout = 2000,
|
|
reconnectTimeout = 2000,
|
|
@@ -21,6 +22,7 @@
|
|
}){
|
|
}){
|
|
this.opts ={
|
|
this.opts ={
|
|
url: url,
|
|
url: url,
|
|
|
|
+ userId:userId,
|
|
pingTimeout: pingTimeout,
|
|
pingTimeout: pingTimeout,
|
|
pongTimeout: pongTimeout,
|
|
pongTimeout: pongTimeout,
|
|
reconnectTimeout: reconnectTimeout,
|
|
reconnectTimeout: reconnectTimeout,
|
|
@@ -34,7 +36,6 @@
|
|
this.onopen = () => {};
|
|
this.onopen = () => {};
|
|
this.onmessage = () => {};
|
|
this.onmessage = () => {};
|
|
this.onreconnect = () => {};
|
|
this.onreconnect = () => {};
|
|
-
|
|
|
|
this.createWebSocket();
|
|
this.createWebSocket();
|
|
}
|
|
}
|
|
WebsocketHeartbeat.prototype.createWebSocket = function(){
|
|
WebsocketHeartbeat.prototype.createWebSocket = function(){
|
|
@@ -80,7 +81,8 @@
|
|
}, this.opts.reconnectTimeout);
|
|
}, this.opts.reconnectTimeout);
|
|
};
|
|
};
|
|
WebsocketHeartbeat.prototype.send = function(msg){
|
|
WebsocketHeartbeat.prototype.send = function(msg){
|
|
- this.ws.send(msg);
|
|
|
|
|
|
+ let msgBean={'cmd':this.opts.pingMsg,'userId':this.opts.userId,'msg':msg};
|
|
|
|
+ this.ws.send(JSON.stringify(msgBean));
|
|
};
|
|
};
|
|
//心跳检测
|
|
//心跳检测
|
|
WebsocketHeartbeat.prototype.heartCheck = function(){
|
|
WebsocketHeartbeat.prototype.heartCheck = function(){
|
|
@@ -92,7 +94,7 @@
|
|
this.pingTimeoutId = setTimeout(() => {
|
|
this.pingTimeoutId = setTimeout(() => {
|
|
//这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
//这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
//onmessage拿到返回的心跳就说明连接正常
|
|
//onmessage拿到返回的心跳就说明连接正常
|
|
- this.ws.send(this.opts.pingMsg);
|
|
|
|
|
|
+ this.send(this.opts.pingMsg);
|
|
//如果超过一定时间还没重置,说明后端主动断开了
|
|
//如果超过一定时间还没重置,说明后端主动断开了
|
|
this.pongTimeoutId = setTimeout(() => {
|
|
this.pongTimeoutId = setTimeout(() => {
|
|
//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
|
|
//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
|