message.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Message = {
  2. MSG_TYPE_TASK: 'task',
  3. MSG_TYPE_ALERT: 'alert',
  4. MSG_TYPE_PROMPT: 'prompt',
  5. menuTree: '',
  6. lockReconnect: false, //避免重复连接
  7. socket: null,
  8. wsUrl: '',
  9. connectWebSocket: function () {
  10. Message.wsUrl = 'ws://' + window.location.host + '/yjyd/websocket';
  11. Message.socket = new WebSocket(Message.wsUrl);
  12. Message.socket.onopen = function (msg) {
  13. Message.heartCheck.reset().start(); //传递信息
  14. console.log('WebSocket opened!');
  15. };
  16. Message.socket.onmessage = function (message) {
  17. Message.heartCheck.reset().start();
  18. if (message.data == 'pong') {
  19. return;
  20. }
  21. var data = JSON.parse(message.data);
  22. Message.messageRouter(data);
  23. console.log('receive message: ' + message.data);
  24. };
  25. Message.socket.onerror = function (error) {
  26. Message.reconnect(Message.wsUrl);
  27. console.log('Error: ' + error.name + error.number);
  28. };
  29. Message.socket.onclose = function () {
  30. Message.reconnect(Message.wsUrl);
  31. console.log('WebSocket closed!');
  32. };
  33. },
  34. messageRouter: function (data) {
  35. switch (data.msgType) {
  36. case Message.MSG_TYPE_TASK: {
  37. Message.cacheTempData(data);
  38. Message.onMessageTask(data);
  39. break;
  40. }
  41. }
  42. },
  43. /**收到任务提示*/
  44. onMessageTask: function (data) {
  45. $.yvan.notify({
  46. autoClose: 5000,
  47. type: 'info',
  48. body: data.msg,
  49. title: data.msgTitle,
  50. onClick: function () {
  51. if (data.data) {
  52. console.log(data.data);
  53. // // 解析路径
  54. // var path = data.data.linkedMenuPath;
  55. // var menuIds = path.split('/');
  56. // for (var i = menuIds.length - 1; i >= 0; i--) {
  57. // if (menuIds[i] == '' || menuIds[i] == null) {
  58. // menuIds.splice(i, 1);
  59. // }
  60. // }
  61. //
  62. // //根据menuId在menuTree里面查找菜单
  63. // if (Message.menuTree == '') {
  64. // return;
  65. // }
  66. // for (var i = 0; i < Message.menuTree.length; i++) {
  67. // var menu1 = Message.menuTree[i];
  68. // // 找到第一级菜单
  69. // if (menu1.id == menuIds[0]) {
  70. // var menu = Message.getTargetMenu(menu1, 1, menuIds);
  71. // App.addTab(
  72. // {
  73. // title: menu.text,
  74. // iconCls: menu.iconCls,
  75. // url: menu.href,
  76. // id: menu.id,
  77. // _: App.currentUrId,
  78. // });
  79. // }
  80. // }
  81. // console.log(menuIds);
  82. top.window.open(".." + data.data.linkedMenuPath);
  83. }
  84. }
  85. });
  86. },
  87. /**递归查找子菜单,直到找到为止*/
  88. getTargetMenu: function (menu, level, menuIds) {
  89. var children = menu.children;
  90. if (children == '' || children == null || menuIds.length < level + 1) {
  91. return menu;
  92. } else {
  93. for (var i = 0; i < children.length; i++) {
  94. var child = children[i];
  95. if (menuIds[level] == child.id) {
  96. level++;
  97. return Message.getTargetMenu(child, level, menuIds);
  98. }
  99. }
  100. }
  101. },
  102. cacheTempData: function (data) {
  103. if (data.data) {
  104. localStorage.setItem("tempData" + top.window.location.host + "/app" + data.data.linkedMenuPath, JSON.stringify(data.data));
  105. }
  106. },
  107. cacheHomeTempData: function (data, patchKey) {
  108. if (data) {
  109. localStorage.setItem("tempData" + top.window.location.host + top.window.location.pathname + "#" + patchKey, JSON.stringify(data));
  110. }
  111. },
  112. getCacheData: function () {
  113. var key = "tempData" + top.window.location.host + top.window.location.pathname + top.window.location.hash;
  114. dataStr = localStorage.getItem(key);
  115. localStorage.removeItem(key);
  116. if (dataStr) {
  117. return JSON.parse(dataStr);
  118. }
  119. return null;
  120. },
  121. getTempParamData: function () {
  122. var d = Message.getCacheData();
  123. if (d) {
  124. return d.param;
  125. }
  126. return null;
  127. },
  128. reconnect: function (url) {
  129. if (Message.lockReconnect) return;
  130. Message.lockReconnect = true;
  131. //没连接上会一直重连,设置延迟避免请求过多
  132. setTimeout(function () {
  133. Message.connectWebSocket();
  134. Message.lockReconnect = false;
  135. }, 2000);
  136. },
  137. //心跳检测
  138. heartCheck: {
  139. timeout: 6000, //60秒
  140. timeoutObj: null,
  141. serverTimeoutObj: null,
  142. reset: function () {
  143. clearTimeout(this.timeoutObj);
  144. clearTimeout(this.serverTimeoutObj);
  145. return this;
  146. },
  147. start: function () {
  148. var self = this;
  149. this.timeoutObj = setTimeout(function () {
  150. //这里发送一个心跳,后端收到后,返回一个心跳消息,
  151. //onmessage拿到返回的心跳就说明连接正常
  152. Message.socket.send("ping");
  153. self.serverTimeoutObj = setTimeout(function () { //如果超过一定时间还没重置,说明后端主动断开了
  154. Message.socket.close(); //如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
  155. }, self.timeout)
  156. }, this.timeout)
  157. }
  158. }
  159. };