Poll.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //轮训处理
  2. Namespace.register("U.MD.O.P");
  3. //设置是否允许连接
  4. U.MD.O.P.isconnect = true;
  5. /*
  6. * 链接后台,并发送请求
  7. */
  8. U.MD.O.P.connect = function () {
  9. if (US.userInfo.userid) { //判断用户是否登录
  10. U.MD.O.P.polling(["login", JSON.stringify({ //发送登录请求
  11. pageId: US.pageId,
  12. userId: US.userInfo.userid,
  13. userName: US.userInfo.username,
  14. type: US.systemId
  15. })]);
  16. }
  17. };
  18. /*
  19. * 数据处理
  20. */
  21. U.MD.O.P.dataAsyn = function () {
  22. }
  23. /*
  24. * 发送请求进入轮训
  25. */
  26. U.MD.O.P.polling = function (params) {
  27. if (U.MD.O.P.isconnect) {
  28. U.MD.O.P.isconnect = false;
  29. params = params || ["polling", US.userInfo.userid, US.pageId, US.systemId]
  30. var start = 0;
  31. var delimiter = "\r\n"
  32. var end = 0;
  33. var text = "";
  34. var xhr = new XMLHttpRequest();
  35. var data = "";
  36. //var time = null;
  37. //
  38. var fun = function () {
  39. //clearTimeout(time)
  40. text = xhr.responseText;
  41. var istrue = true;
  42. while (start < text.length && istrue) {
  43. end = text.indexOf(delimiter, start);//获取数据的是否已经结束
  44. if (end > -1) {
  45. data += text.substring(start, end);
  46. start = end + delimiter.length;
  47. try {
  48. U.MD.O.P.pollingAsyn({ value: JSON.parse(data) })
  49. }
  50. catch (e) {
  51. console.log(e);
  52. }
  53. data = "";
  54. }
  55. else {
  56. istrue = false;
  57. //time = setTimeout(fun, 500);//
  58. }
  59. }
  60. };
  61. xhr.open("POST", US.SCOKET, true);
  62. xhr.onreadystatechange = function () {
  63. //获取数据
  64. if (xhr.readyState == 3 && xhr.status == 200) {
  65. fun();
  66. }
  67. //结束重连
  68. else if (xhr.readyState == 4) {
  69. console.log(xhr);
  70. U.MD.O.P.isconnect = true;
  71. U.MD.O.P.connect();
  72. }
  73. };
  74. //超时重新连接
  75. xhr.onerror = function (err) {
  76. console.log(err);
  77. };
  78. try {
  79. //发送请求
  80. xhr.send(U.A.Request.toAjaxFormat(params));
  81. }
  82. catch (e) {
  83. console.log(e);
  84. }
  85. }
  86. }
  87. /*
  88. * 处理从后台返回的数据
  89. *
  90. */
  91. U.MD.O.P.pollingAsyn = function (r) {
  92. r = r.value;
  93. if (r) { //r是否有值
  94. if (r[0] instanceof Array) {
  95. r = r[0];
  96. }
  97. // console.log('获取到的数据:', r)
  98. var _i, _message, _iframe, _contentWindow;
  99. for (_i = 0; _i < r.length; _i++) {
  100. _message = r[_i];
  101. switch (_message.type) {
  102. case 'us.word': //判断是否是word 的消息
  103. // _iframe = $('#UD_Word' + _message.messageInfo.docId + ' iframe')[0];
  104. _iframe = $('#doc iframe')[0];
  105. if (_iframe) {
  106. _contentWindow = _iframe.contentWindow;
  107. if (_message.messageInfo.type == "updateName") {
  108. $('#UD_Word' + _message.messageInfo.docId + ' .U_MD_O_H_head_navigation_Title')[0].value = _message.messageInfo.name;
  109. }
  110. else if (_message.messageInfo.type == "insertModifyRecord") {
  111. _contentWindow.U.MD.O.W.Panel.syncHistory(_message.sendId, _message.messageInfo.historyid);
  112. }
  113. else if (_message.messageInfo.type == "addUser") {
  114. _contentWindow.U.MD.O.W.Panel.syncAddUser(_message.messageInfo.userIds, _message.sendId);
  115. }
  116. else {// if ($('#UD_Word' + _message.messageInfo.docId).css('display') == 'block')
  117. switch (_message.messageInfo.type) {// 判断消息种类
  118. case 'update':
  119. _contentWindow.U.MD.O.W.E.updateEditorLine(_message.messageInfo);
  120. break;
  121. case 'add':
  122. _contentWindow.U.MD.O.W.E.addEditorLine(_message.messageInfo);
  123. break;
  124. case 'delete':
  125. _contentWindow.U.MD.O.W.E.deleteEditorLine(_message.messageInfo);
  126. break;
  127. }
  128. }
  129. }
  130. break;
  131. case 'us.nav':
  132. _iframe = $('#UD_Word' + _message.messageInfo.docId + ' iframe')[0];
  133. if (_message.messageInfo.type == "updateName") {
  134. $('#UD_Word' + _message.messageInfo.docId + ' .U_MD_O_H_head_navigation_Title')[0].value = _message.messageInfo.name;
  135. } else if (_message.messageInfo.type == "addUser") {
  136. _contentWindow.U.MD.O.W.Panel.syncAddUser(_message.messageInfo.userIds, _message.sendId);
  137. }
  138. else if ($('#UD_Word' + _message.messageInfo.docId).css('display') == 'block') {
  139. _contentWindow = _iframe.contentWindow;
  140. switch (_message.messageInfo.type) {// 判断消息种类
  141. case 'update':
  142. _contentWindow.U.MD.O.W.T.N.nowNavId == _message.messageInfo.navId && _contentWindow.U.MD.O.W.E.updateEditorLine(_message.messageInfo);
  143. break;
  144. case 'add':
  145. _contentWindow.U.MD.O.W.T.N.nowNavId == _message.messageInfo.navId && _contentWindow.U.MD.O.W.E.addEditorLine(_message.messageInfo);
  146. break;
  147. case 'delete':
  148. _contentWindow.U.MD.O.W.T.N.nowNavId == _message.messageInfo.navId && _contentWindow.U.MD.O.W.E.deleteEditorLine(_message.messageInfo);
  149. break;
  150. case 'addNav':
  151. _contentWindow.U.MD.O.W.T.N.Poll.addNav(_message.messageInfo);
  152. break;
  153. case 'updateNav':
  154. _contentWindow.U.MD.O.W.T.N.Poll.updateNav(_message.messageInfo);
  155. break;
  156. case 'deleteNav':
  157. _contentWindow.U.MD.O.W.T.N.Poll.deleteNav(_message.messageInfo);
  158. break;
  159. case 'updateLogo':
  160. _contentWindow.U.MD.O.W.T.N.Poll.updateLogo(_message.messageInfo);
  161. break;
  162. }
  163. }
  164. break;
  165. case 'us.excel': //判断是否是word 的消息
  166. _iframe = $('#UD_Excel' + _message.messageInfo.docId + ' iframe')[0];
  167. if (_iframe) {
  168. if (_message.messageInfo.type == "updateName") {
  169. $('#UD_Excel' + _message.messageInfo.docId + ' .U_MD_O_H_head_navigation_Title')[0].value = _message.messageInfo.name;
  170. }
  171. else if (_message.messageInfo.type == "addUser") {
  172. }
  173. else {
  174. _contentWindow = _iframe.contentWindow;
  175. switch (_message.messageInfo.type) {// 判断消息种类
  176. case 'blur':
  177. _contentWindow.U.MD.O.E.Receiver.Blur(_message.messageInfo);
  178. break;
  179. case 'setBorder':
  180. _contentWindow.U.MD.O.E.Receiver.setBorder(_message.messageInfo);
  181. break;
  182. case "addLine":
  183. _contentWindow.U.MD.O.E.Receiver.addLine(_message.messageInfo);
  184. //添加行
  185. break;
  186. case "delLine":
  187. _contentWindow.U.MD.O.E.Receiver.delLine(_message.messageInfo);
  188. //刪除行
  189. break;
  190. case "addCol":
  191. _contentWindow.U.MD.O.E.Receiver.addCol(_message.messageInfo);
  192. //添加列
  193. break;
  194. case "delCol":
  195. _contentWindow.U.MD.O.E.Receiver.delCol(_message.messageInfo);
  196. //刪除列
  197. break;
  198. case "merge":
  199. _contentWindow.U.MD.O.E.Receiver.merge(_message.messageInfo);
  200. //合并单元格
  201. break;
  202. case "splitCells":
  203. _contentWindow.U.MD.O.E.Receiver.splitCells(_message.messageInfo);
  204. //拆分单元格
  205. break;
  206. case "rowExpanding":
  207. _contentWindow.U.MD.O.E.Receiver.rowExpanding(_message.messageInfo);
  208. //行板拉伸
  209. break;
  210. case "colExpanding":
  211. _contentWindow.U.MD.O.E.Receiver.colExpanding(_message.messageInfo);
  212. //列板拉伸
  213. break;
  214. case "addTask":
  215. _contentWindow.U.MD.O.E.Receiver.addTask(_message.messageInfo);
  216. break;
  217. case "deleteTask":
  218. _contentWindow.U.MD.O.E.Receiver.deleteTask(_message.messageInfo);
  219. break;
  220. case "reNameTask":
  221. _contentWindow.U.MD.O.E.Receiver.reNameTask(_message.messageInfo);
  222. break;
  223. case "img":
  224. _contentWindow.U.MD.O.E.Receiver.img(_message.messageInfo);
  225. //插入图片
  226. break;
  227. case "imgChange": //图片拉伸及拖拽
  228. _contentWindow.U.MD.O.E.Receiver.imgChange(_message.messageInfo);
  229. break;
  230. case "excelAddHref": //插入链接
  231. _contentWindow.U.MD.O.E.Receiver.excelAddHref(_message.messageInfo);
  232. break;
  233. case "clearAContent": //删除链接
  234. _contentWindow.U.MD.O.E.Receiver.clearAContent(_message.messageInfo);
  235. break;
  236. }
  237. }
  238. }
  239. break;
  240. case 'us.mindNetwork':
  241. // _iframe = $('#mindNetwork' + _message.messageInfo.docId)[0];
  242. _iframe = $('#mindNetwork iframe')[0];
  243. if (_iframe) {
  244. _contentWindow = _iframe.contentWindow;
  245. // if (_message.messageInfo.type == "addUser") {
  246. // _contentWindow.postMessage({ cid: cid, type: "mindNetwork_addUser" }, "*")
  247. // }
  248. // else
  249. // if ($('#mindNetwork' + _message.messageInfo.docId).css('display') == 'block') {
  250. switch (_message.messageInfo.type) {// 判断消息种类
  251. case 'update':
  252. _contentWindow.postMessage({ info: _message.messageInfo, type: "mindNetwork_update" }, "*")
  253. break;
  254. case 'delete':
  255. _contentWindow.postMessage({ info: _message.messageInfo, type: "mindNetwork_delete" }, "*")
  256. break;
  257. case 'move':
  258. _contentWindow.postMessage({ info: _message.messageInfo, type: "mindNetwork_move" }, "*")
  259. break;
  260. }
  261. // }
  262. }
  263. break;
  264. case 'us.realTimeClass':
  265. _iframe = $('#studentClassRoom iframe')[0];
  266. if (_iframe) {
  267. _contentWindow = _iframe.contentWindow;
  268. switch (_message.messageInfo.type) {// 判断消息种类
  269. case 'update'://修改页码
  270. _contentWindow.postMessage({ info: _message.messageInfo, type: "realTimeClass_update" }, "*")
  271. break;
  272. }
  273. // }
  274. }
  275. break;
  276. case 'us.cscl':
  277. _iframe = $('#futureClass iframe')[0];
  278. if (_iframe) {
  279. _contentWindow = _iframe.contentWindow;
  280. _contentWindow.postMessage({ info: _message.messageInfo, type: _message.messageInfo.type }, "*");
  281. }
  282. break;
  283. }
  284. }
  285. }
  286. //U.MD.O.P.polling(); //进入轮循
  287. }