You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.2 KiB

  1. function uuid() {
  2. return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
  3. (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  4. )
  5. }
  6. var ZWSURL = "ws://" + window.location.hostname + ":" + window.location.port + "/ws/cmd";
  7. class ZCommand {
  8. /*******************************************************************************************************************
  9. * ===================================================BASE======================================================== *
  10. *******************************************************************************************************************/
  11. constructor() {
  12. if (!('WebSocket' in window)) {
  13. alert('当前浏览器 Not support websocket')
  14. }
  15. this.connected = false;
  16. this.connecting = false;
  17. this.cmdServerListener = new Map();
  18. this.onConnect;
  19. this.onReceipts = [];
  20. this.onReports = [];
  21. this.onSendraws = [];
  22. this.onConnects = [];
  23. this.onDisConconnects = [];
  24. }
  25. call_onReceipts(message, receipt) {
  26. for (var i = 0; i < this.onReceipts.length; i++) {
  27. this.onReceipts[i](message, receipt);
  28. }
  29. }
  30. call_onReports(message) {
  31. for (var i = 0; i < this.onReports.length; i++) {
  32. this.onReports[i](message);
  33. }
  34. }
  35. call_onSendraw(message) {
  36. for (var i = 0; i < this.onSendraws.length; i++) {
  37. this.onSendraws[i](message);
  38. }
  39. }
  40. call_onConnect() {
  41. for (var i = 0; i < this.onConnects.length; i++) {
  42. this.onConnects[i]();
  43. }
  44. }
  45. call_onDisConconnect() {
  46. for (var i = 0; i < this.onDisConconnects.length; i++) {
  47. this.onDisConconnects[i]();
  48. }
  49. }
  50. start_auto_connect(wsurl, onFirstConnect) {
  51. var ThisClass = this;
  52. this.onFirstConnect = onFirstConnect;
  53. this.wsurl = wsurl;
  54. ThisClass.connectToCmdServer();
  55. // setInterval(function () {
  56. // if (!ThisClass.isConnect()) {
  57. // console.info("ZcmdService try connect to server");
  58. // ThisClass.connectToCmdServer();
  59. // }
  60. // }, 2000);
  61. }
  62. isConnect() {
  63. return this.connected;
  64. }
  65. isConnecting() {
  66. return this.connecting;
  67. }
  68. connectToCmdServer() {
  69. this.connecting = true;
  70. console.info('websocket create socket:' + this.wsurl);
  71. this.cmdServer = new WebSocket(this.wsurl);
  72. var ThisClass = this;
  73. this.cmdServer.onopen = function () {
  74. ThisClass.connecting = false;
  75. ThisClass.connected = true;
  76. console.info("ZcmdService connect to server");
  77. ThisClass.call_onConnect();
  78. }
  79. this.cmdServer.onerror = function () {
  80. ThisClass.connecting = false;
  81. ThisClass.connected = false;
  82. console.error("ZcmdService connect to server error");
  83. ThisClass.connectToCmdServer();
  84. };
  85. var ThisClass = this;
  86. this.cmdServer.onmessage = function (event) {
  87. var obj = $.parseJSON(event.data);
  88. var messageId = obj["messageId"];
  89. if (ThisClass.cmdServerListener.has(messageId)) {
  90. var txcontext = ThisClass.cmdServerListener.get(messageId);
  91. txcontext.onReceipt(txcontext, obj);
  92. ThisClass.cmdServerListener.delete(messageId);
  93. } else {
  94. ThisClass.call_onReports(obj);
  95. }
  96. }
  97. this.cmdServer.onclose = function () {
  98. ThisClass.connected = false;
  99. console.warn("websocket close");
  100. ThisClass.call_onDisConconnect();
  101. }
  102. };
  103. sendCmd(txcontext) {
  104. var ThisClass = this;
  105. var messageId = uuid();;
  106. txcontext.message["messageId"] = messageId;
  107. txcontext.message["need_receipt"] = true;
  108. // TODO:这里修改成异步等待链接成功,并发送
  109. if (!this.isConnect()) {
  110. console.error("Servcer is unconnected");
  111. setTimeout(function () {
  112. txcontext.onOvertime();
  113. }, txcontext.overtime)
  114. return;
  115. }
  116. txcontext.timerId = setTimeout(function () {
  117. console.error("cmd is overtime");
  118. txcontext.onOvertime();
  119. ThisClass.cmdServerListener.delete(messageId);
  120. }, txcontext.overtime)
  121. txcontext.onReceipt = function (_txcontext, receipt) {
  122. clearTimeout(_txcontext.timerId)
  123. _txcontext.onMessage(receipt)
  124. ThisClass.call_onReceipts(_txcontext.message, receipt)
  125. }
  126. this.cmdServerListener.set(messageId, txcontext);
  127. this.call_onSendraw(txcontext.message);
  128. this.cmdServer.send(JSON.stringify(txcontext.message));
  129. };
  130. /*******************************************************************************************************************
  131. * =================================================通用发送消息================================================== *
  132. *******************************************************************************************************************/
  133. send_message(jsonobject, _overtime = 3000) {
  134. var p = new Promise(
  135. (resolve, reject) => {
  136. this.sendCmd({
  137. message: jsonobject,
  138. onMessage: function (message) {
  139. if (resolve)
  140. resolve(message);
  141. },
  142. onOvertime: function () { },
  143. onError: function () { },
  144. overtime: _overtime,
  145. });
  146. }
  147. )
  148. return p;
  149. };
  150. set_onReceipt(onReceipt) {
  151. this.onReceipts.push(onReceipt);
  152. }
  153. set_onReport(onReport) {
  154. this.onReports.push(onReport);
  155. }
  156. set_onSendraw(onSendraw) {
  157. this.onSendraws.push(onSendraw);
  158. }
  159. set_onConnect(onConnect) {
  160. this.onConnects.push(onConnect);
  161. }
  162. set_onDisConconnect(onDisConconnect) {
  163. this.onDisConconnects.push(onDisConconnect);
  164. }
  165. }
  166. // var zcmd = new ZCommand();
  167. // zcmd.start_auto_connect(() => { });