forked from mirror/MCSManager
add HeartBeatPackage
This commit is contained in:
parent
9a64e920d9
commit
936b87fe0b
@ -1,6 +1,16 @@
|
||||
;
|
||||
(function () {
|
||||
|
||||
|
||||
var DEBUG = false; //Websocket DEBUG
|
||||
|
||||
//from @BBleae
|
||||
function wsHeartBeatPackage(ws) {
|
||||
setInterval(function () {
|
||||
console.log("--HeartBeatPackage--");
|
||||
ws.sendMsg("HeartBeatPackage", "");
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
var ify = '\n\n';
|
||||
window.WS = new Object();
|
||||
|
||||
@ -8,8 +18,9 @@
|
||||
var wsURL = 'websocket/ws?' + RES.TOKEN_NAME + '=' + RES.TOKEN;
|
||||
window.WS = new WebSocket(MCSERVER.URL(wsURL, 'ws://'));
|
||||
var tmp_callback = null;
|
||||
wsHeartBeatPackage(WS); //心跳包定时器开启
|
||||
|
||||
WS.onmessage = function (e) {
|
||||
// console.log('_message:'+e.data);
|
||||
var data = e.data;
|
||||
var loc = data.indexOf('\n\n');
|
||||
var header = data.substr(0, loc);
|
||||
@ -17,20 +28,20 @@
|
||||
var obj;
|
||||
try {
|
||||
obj = JSON.parse(header);
|
||||
console.log('--------------- Websocket收到触发 ---------------')
|
||||
console.log(obj);
|
||||
console.log('身体:' + body);
|
||||
console.log('--------------- Websocket收到结束 ---------------')
|
||||
if (DEBUG) {
|
||||
console.log('--------------- Websocket收到触发 ---------------')
|
||||
console.log(obj);
|
||||
console.log('Body:' + body);
|
||||
console.log('--------------- Websocket收到结束 ---------------')
|
||||
}
|
||||
obj.body = body;
|
||||
MI.on('ws/response', obj, body);
|
||||
tmp_callback && tmp_callback();
|
||||
tmp_callback = null;
|
||||
// MI.routeOn(obj['ResponseKey'], obj['ResponseValue']);
|
||||
} catch (e) {
|
||||
console.log('Websocket 收到的数据异常:');
|
||||
console.log('收到的数据是:' + e.data)
|
||||
console.log(e)
|
||||
|
||||
}
|
||||
};
|
||||
WS.onerror = function (err) {
|
||||
@ -49,20 +60,20 @@
|
||||
RequestKey: 'req',
|
||||
RequestValue: value
|
||||
}
|
||||
console.log('--------------- Websocket发送触发 ---------------')
|
||||
console.log(obj);
|
||||
console.log(body);
|
||||
console.log('--------------- Websocket发送结束 ---------------')
|
||||
if (DEBUG) {
|
||||
console.log('--------------- Websocket发送触发 ---------------');
|
||||
console.log(obj);
|
||||
console.log(body);
|
||||
console.log('--------------- Websocket发送结束 ---------------');
|
||||
}
|
||||
var headerStr = JSON.stringify(obj);
|
||||
body = body || '';
|
||||
if (callback) tmp_callback = callback;
|
||||
if (WS.readyState == WS.OPEN) {
|
||||
WS.send(headerStr + ify + body);
|
||||
} else {
|
||||
console.log('############# Websocket 已经关闭 #################');
|
||||
TOOLS.pushMsgWindow('与服务器链接中断,数据发送失败,请刷新或登陆重试')
|
||||
TOOLS.pushMsgWindow('与服务器链接中断,数据发送失败,请刷新或登陆重试');
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user