Message = { MSG_TYPE_TASK: 'task', MSG_TYPE_ALERT: 'alert', MSG_TYPE_PROMPT: 'prompt', menuTree: '', lockReconnect: false, //避免重复连接 socket: null, wsUrl: '', connectWebSocket: function () { Message.wsUrl = 'ws://' + window.location.host + '/sys/websocket'; Message.socket = new WebSocket(Message.wsUrl); Message.socket.onopen = function (msg) { Message.heartCheck.reset().start(); //传递信息 console.log('WebSocket opened!'); }; Message.socket.onmessage = function (message) { Message.heartCheck.reset().start(); if (message.data == 'pong') { return; } var data = JSON.parse(message.data); Message.messageRouter(data); console.log('receive message: ' + message.data); }; Message.socket.onerror = function (error) { Message.reconnect(Message.wsUrl); console.log('Error: ' + error.name + error.number); }; Message.socket.onclose = function () { Message.reconnect(Message.wsUrl); console.log('WebSocket closed!'); }; }, messageRouter: function (data) { switch (data.msgType) { case Message.MSG_TYPE_TASK: { Message.cacheTempData(data); Message.onMessageTask(data); break; } } }, /**收到任务提示*/ onMessageTask: function (data) { $.yvan.notify({ autoClose: 5000, type: 'info', body: data.msg, title: data.msgTitle, onClick: function () { if (data.data) { console.log(data.data); // // 解析路径 // var path = data.data.linkedMenuPath; // var menuIds = path.split('/'); // for (var i = menuIds.length - 1; i >= 0; i--) { // if (menuIds[i] == '' || menuIds[i] == null) { // menuIds.splice(i, 1); // } // } // // //根据menuId在menuTree里面查找菜单 // if (Message.menuTree == '') { // return; // } // for (var i = 0; i < Message.menuTree.length; i++) { // var menu1 = Message.menuTree[i]; // // 找到第一级菜单 // if (menu1.id == menuIds[0]) { // var menu = Message.getTargetMenu(menu1, 1, menuIds); // App.addTab( // { // title: menu.text, // iconCls: menu.iconCls, // url: menu.href, // id: menu.id, // _: App.currentUrId, // }); // } // } // console.log(menuIds); top.window.open(".." + data.data.linkedMenuPath); } } }); }, /**递归查找子菜单,直到找到为止*/ getTargetMenu: function (menu, level, menuIds) { var children = menu.children; if (children == '' || children == null || menuIds.length < level + 1) { return menu; } else { for (var i = 0; i < children.length; i++) { var child = children[i]; if (menuIds[level] == child.id) { level++; return Message.getTargetMenu(child, level, menuIds); } } } }, cacheTempData: function (data) { if (data.data) { localStorage.setItem("tempData" + top.window.location.host + "/app" + data.data.linkedMenuPath, JSON.stringify(data.data)); } }, cacheHomeTempData: function (data, patchKey) { if (data) { localStorage.setItem("tempData" + top.window.location.host + top.window.location.pathname + "#" + patchKey, JSON.stringify(data)); } }, getCacheData: function () { var key = "tempData" + top.window.location.host + top.window.location.pathname + top.window.location.hash; dataStr = localStorage.getItem(key); localStorage.removeItem(key); if (dataStr) { return JSON.parse(dataStr); } return null; }, getTempParamData: function () { var d = Message.getCacheData(); if (d) { return d.param; } return null; }, reconnect: function (url) { if (Message.lockReconnect) return; Message.lockReconnect = true; //没连接上会一直重连,设置延迟避免请求过多 setTimeout(function () { Message.connectWebSocket(); Message.lockReconnect = false; }, 2000); }, //心跳检测 heartCheck: { timeout: 6000, //60秒 timeoutObj: null, serverTimeoutObj: null, reset: function () { clearTimeout(this.timeoutObj); clearTimeout(this.serverTimeoutObj); return this; }, start: function () { var self = this; this.timeoutObj = setTimeout(function () { //这里发送一个心跳,后端收到后,返回一个心跳消息, //onmessage拿到返回的心跳就说明连接正常 Message.socket.send("ping"); self.serverTimeoutObj = setTimeout(function () { //如果超过一定时间还没重置,说明后端主动断开了 Message.socket.close(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次 }, self.timeout) }, this.timeout) } } };