a398b03a98801c1674542f8831e019207241c4be.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, Logger, WebSock, _crd;
  4. function _reportPossibleCrUseOfLogger(extras) {
  5. _reporterNs.report("Logger", "../../core/common/log/Logger", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfISocket(extras) {
  8. _reporterNs.report("ISocket", "./NetInterface", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfMessageFunc(extras) {
  11. _reporterNs.report("MessageFunc", "./NetInterface", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfNetData(extras) {
  14. _reporterNs.report("NetData", "./NetInterface", _context.meta, extras);
  15. }
  16. _export("WebSock", void 0);
  17. return {
  18. setters: [function (_unresolved_) {
  19. _reporterNs = _unresolved_;
  20. }, function (_cc) {
  21. _cclegacy = _cc.cclegacy;
  22. }, function (_unresolved_2) {
  23. Logger = _unresolved_2.Logger;
  24. }],
  25. execute: function () {
  26. _crd = true;
  27. _cclegacy._RF.push({}, "70df2VbIU9B66Fr+op8FKJp", "WebSock", undefined);
  28. /*
  29. * @Author: dgflash
  30. * @Date: 2021-07-03 16:13:17
  31. * @LastEditors: dgflash
  32. * @LastEditTime: 2022-09-09 17:42:19
  33. */
  34. /**
  35. * WebSocket 封装
  36. * 1. 连接/断开相关接口
  37. * 2. 网络异常回调
  38. * 3. 数据发送与接收
  39. */
  40. _export("WebSock", WebSock = class WebSock {
  41. constructor() {
  42. this._ws = null;
  43. // websocket对象
  44. /** 网络连接成功事件 */
  45. this.onConnected = null;
  46. /** 接受到网络数据事件 */
  47. this.onMessage = null;
  48. /** 网络错误事件 */
  49. this.onError = null;
  50. /** 网络断开事件 */
  51. this.onClosed = null;
  52. }
  53. /** 请求连接 */
  54. connect(options) {
  55. if (this._ws) {
  56. if (this._ws.readyState === WebSocket.CONNECTING) {
  57. (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
  58. error: Error()
  59. }), Logger) : Logger).logNet("websocket connecting, wait for a moment...");
  60. return false;
  61. }
  62. }
  63. let url = null;
  64. if (options.url) {
  65. url = options.url;
  66. } else {
  67. let ip = options.ip;
  68. let port = options.port;
  69. let protocol = options.protocol;
  70. url = `${protocol}://${ip}:${port}`;
  71. }
  72. this._ws = new WebSocket(url);
  73. this._ws.binaryType = options.binaryType ? options.binaryType : "arraybuffer";
  74. this._ws.onmessage = event => {
  75. let onMessage = this.onMessage;
  76. onMessage(event.data);
  77. };
  78. this._ws.onopen = this.onConnected;
  79. this._ws.onerror = this.onError;
  80. this._ws.onclose = this.onClosed;
  81. return true;
  82. }
  83. /**
  84. * 发送数据
  85. * @param buffer 网络数据
  86. */
  87. send(buffer) {
  88. if (this._ws && this._ws.readyState == WebSocket.OPEN) {
  89. this._ws.send(buffer);
  90. return 1;
  91. }
  92. return -1;
  93. }
  94. /**
  95. * 网络断开
  96. * @param code 关闭码
  97. * @param reason 关闭原因
  98. */
  99. close(code, reason) {
  100. if (this._ws) {
  101. this._ws.close(code, reason);
  102. }
  103. }
  104. });
  105. _cclegacy._RF.pop();
  106. _crd = false;
  107. }
  108. };
  109. });
  110. //# sourceMappingURL=a398b03a98801c1674542f8831e019207241c4be.js.map