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.

148 lines
5.3 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.firstConnect = true;
  20. }
  21. start_auto_connect(wsurl, onFirstConnect) {
  22. var ThisClass = this;
  23. this.onFirstConnect = onFirstConnect;
  24. this.wsurl = wsurl;
  25. ThisClass.connectToCmdServer();
  26. setInterval(function () {
  27. if (!ThisClass.isConnect()) {
  28. console.info("ZcmdService try connect to server");
  29. ThisClass.connectToCmdServer();
  30. }
  31. }, 2000);
  32. }
  33. isConnect() {
  34. return this.connected;
  35. }
  36. isConnecting() {
  37. return this.connecting;
  38. }
  39. connectToCmdServer() {
  40. this.connecting = true;
  41. console.info('websocket create socket:' + this.wsurl);
  42. this.cmdServer = new WebSocket(this.wsurl);
  43. var ThisClass = this;
  44. this.cmdServer.onopen = function () {
  45. ThisClass.connecting = false;
  46. ThisClass.connected = true;
  47. console.info("ZcmdService connect to server");
  48. if (ThisClass.firstConnect && ThisClass.onFirstConnect) {
  49. ThisClass.onFirstConnect();
  50. }
  51. ThisClass.firstConnect = false;
  52. }
  53. this.cmdServer.onerror = function () {
  54. ThisClass.connecting = false;
  55. ThisClass.connected = false;
  56. console.info("websocket onerror");
  57. };
  58. var ThisClass = this;
  59. this.cmdServer.onmessage = function (event) {
  60. var obj = $.parseJSON(event.data);
  61. var messageId = obj["messageId"];
  62. if (ThisClass.cmdServerListener.has(messageId)) {
  63. var txcontext = ThisClass.cmdServerListener.get(messageId);
  64. txcontext.onreceipt(txcontext, obj);
  65. ThisClass.cmdServerListener.delete(messageId);
  66. } else {
  67. if (ThisClass.onreport) {
  68. ThisClass.onreport(obj)
  69. }
  70. }
  71. }
  72. this.cmdServer.onclose = function () {
  73. ThisClass.connected = false;
  74. console.warn("websocket close");
  75. }
  76. };
  77. sendCmd(txcontext) {
  78. var ThisClass = this;
  79. var messageId = uuid();;
  80. txcontext.message["messageId"] = messageId;
  81. txcontext.message["need_receipt"] = true;
  82. // TODO:这里修改成异步等待链接成功,并发送
  83. if (!this.isConnect()) {
  84. console.error("Servcer is unconnected");
  85. setTimeout(function () {
  86. txcontext.onOvertime();
  87. }, txcontext.overtime)
  88. return;
  89. }
  90. txcontext.timerId = setTimeout(function () {
  91. console.error("cmd is overtime");
  92. txcontext.onOvertime();
  93. ThisClass.cmdServerListener.delete(messageId);
  94. }, txcontext.overtime)
  95. txcontext.onreceipt = function (_txcontext, receipt) {
  96. clearTimeout(_txcontext.timerId)
  97. _txcontext.onMessage(receipt)
  98. if (ThisClass.onreceipt) {
  99. ThisClass.onreceipt(_txcontext.message, receipt)
  100. }
  101. }
  102. this.cmdServerListener.set(messageId, txcontext);
  103. if (this.onsendraw) {
  104. this.onsendraw(txcontext.message);
  105. }
  106. this.cmdServer.send(JSON.stringify(txcontext.message));
  107. };
  108. /*******************************************************************************************************************
  109. * =================================================通用发送消息================================================== *
  110. *******************************************************************************************************************/
  111. send_message(jsonobject, _overtime = 3000) {
  112. var p = new Promise(
  113. (resolve, reject) => {
  114. this.sendCmd({
  115. message: jsonobject,
  116. onMessage: function (message) {
  117. if (resolve)
  118. resolve(message);
  119. },
  120. onOvertime: function () { },
  121. onError: function () { },
  122. overtime: _overtime,
  123. });
  124. }
  125. )
  126. return p;
  127. };
  128. set_onreceipt(onreceipt) {
  129. this.onreceipt = onreceipt;
  130. }
  131. set_onreport(onreport) {
  132. this.onreport = onreport;
  133. }
  134. set_onsendraw(onsendraw) {
  135. this.onsendraw = onsendraw;
  136. }
  137. }
  138. // var zcmd = new ZCommand();
  139. // zcmd.start_auto_connect(() => { });