50218543dde2ce9125518fbbc4803ace959dbdb6.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. System.register(["__unresolved_0", "cc"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, NetManager, _crd;
  4. function _reportPossibleCrUseOfCallbackObject(extras) {
  5. _reporterNs.report("CallbackObject", "./NetInterface", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfIRequestProtocol(extras) {
  8. _reporterNs.report("IRequestProtocol", "./NetInterface", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfNetData(extras) {
  11. _reporterNs.report("NetData", "./NetInterface", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfNetConnectOptions(extras) {
  14. _reporterNs.report("NetConnectOptions", "./NetNode", _context.meta, extras);
  15. }
  16. function _reportPossibleCrUseOfNetNode(extras) {
  17. _reporterNs.report("NetNode", "./NetNode", _context.meta, extras);
  18. }
  19. _export("NetManager", void 0);
  20. return {
  21. setters: [function (_unresolved_) {
  22. _reporterNs = _unresolved_;
  23. }, function (_cc) {
  24. _cclegacy = _cc.cclegacy;
  25. }],
  26. execute: function () {
  27. _crd = true;
  28. _cclegacy._RF.push({}, "d8cd5el6GBGTYTW+N8b8EuJ", "NetManager", undefined);
  29. /*
  30. * @Author: dgflash
  31. * @Date: 2022-09-01 18:00:28
  32. * @LastEditors: dgflash
  33. * @LastEditTime: 2022-09-09 18:10:50
  34. */
  35. /**
  36. * 使用流程文档可参考、简化与服务器对接、使用新版API体验,可进入下面地址获取新版本,替换network目录中的内容
  37. * https://store.cocos.com/app/detail/5877
  38. */
  39. /*
  40. * 网络节点管理类
  41. */
  42. _export("NetManager", NetManager = class NetManager {
  43. constructor() {
  44. this._channels = {};
  45. }
  46. /** 网络管理单例对象 */
  47. static getInstance() {
  48. if (!this._instance) {
  49. this._instance = new NetManager();
  50. }
  51. return this._instance;
  52. }
  53. /**
  54. * 添加网络节点
  55. * @param node 网络节点
  56. * @param channelId 通道编号
  57. * @example
  58. // 游戏服务器心跳协议
  59. class GameProtocol extends NetProtocolPako {
  60. // 自定义心跳协议
  61. getHearbeat(): NetData {
  62. return '{"action":"LoginAction","method":"heart","data":"null","callback":"LoginAction_heart"}';
  63. }
  64. }
  65. var net = new NetNodeGame();
  66. var ws = new WebSock(); // WebSocket 网络连接对象
  67. var gp = new GameProtocol(); // 网络通讯协议对象
  68. var gt = new NetGameTips() // 网络提示对象
  69. net.init(ws, gp, gt);
  70. NetManager.getInstance().setNetNode(net, NetChannelType.Game);
  71. */
  72. setNetNode(node, channelId = 0) {
  73. this._channels[channelId] = node;
  74. }
  75. /** 移除Node */
  76. removeNetNode(channelId) {
  77. delete this._channels[channelId];
  78. }
  79. /**
  80. * 网络节点连接服务器
  81. * @param options 连接参数
  82. * @param channelId 通道编号
  83. * @example
  84. var options = {
  85. url: 'ws://127.0.0.1:3000',
  86. autoReconnect: 0 // -1 永久重连,0不自动重连,其他正整数为自动重试次数
  87. }
  88. NetManager.getInstance().connect(options, NetChannelType.Game);
  89. */
  90. connect(options, channelId = 0) {
  91. if (this._channels[channelId]) {
  92. return this._channels[channelId].connect(options);
  93. }
  94. return false;
  95. }
  96. /** 节点连接发送数据*/
  97. send(buf, force = false, channelId = 0) {
  98. let node = this._channels[channelId];
  99. if (node) {
  100. return node.send(buf, force);
  101. }
  102. return -1;
  103. }
  104. /**
  105. * 发起请求,并在在结果返回时调用指定好的回调函数
  106. * @param reqProtocol 请求协议
  107. * @param rspObject 回调对象
  108. * @param showTips 是否触发请求提示
  109. * @param force 是否强制发送
  110. * @param channelId 通道编号
  111. * @example
  112. let protocol: IRequestProtocol = {
  113. action: action,
  114. method: method,
  115. data: JSON.stringify(data),
  116. isCompress: this.isCompress,
  117. channelid: netConfig.channelid
  118. }
  119. return this.request(protocol, rspObject, showTips, force);
  120. */
  121. request(reqProtocol, rspObject, showTips = true, force = false, channelId = 0) {
  122. let node = this._channels[channelId];
  123. if (node) {
  124. node.request(reqProtocol, rspObject, showTips, force);
  125. }
  126. }
  127. /**
  128. * 同request功能一致,但在request之前会先判断队列中是否已有rspCmd,如有重复的则直接返回
  129. * @param reqProtocol 请求协议
  130. * @param rspObject 回调对象
  131. * @param showTips 是否触发请求提示
  132. * @param force 是否强制发送
  133. * @param channelId 通道编号
  134. * @example
  135. let protocol: IRequestProtocol = {
  136. action: action,
  137. method: method,
  138. data: JSON.stringify(data),
  139. isCompress: this.isCompress,
  140. channelid: netConfig.channelid
  141. }
  142. return this.request(protocol, rspObject, showTips, force);
  143. */
  144. requestUnique(reqProtocol, rspObject, showTips = true, force = false, channelId = 0) {
  145. let node = this._channels[channelId];
  146. if (node) {
  147. return node.requestUnique(reqProtocol, rspObject, showTips, force);
  148. }
  149. return false;
  150. }
  151. /**
  152. * 节点网络断开
  153. * @param code 关闭码
  154. * @param reason 关闭原因
  155. * @param channelId 通道编号
  156. * @example
  157. * NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
  158. */
  159. close(code, reason, channelId = 0) {
  160. if (this._channels[channelId]) {
  161. return this._channels[channelId].closeSocket(code, reason);
  162. }
  163. }
  164. });
  165. NetManager._instance = void 0;
  166. _cclegacy._RF.pop();
  167. _crd = false;
  168. }
  169. };
  170. });
  171. //# sourceMappingURL=50218543dde2ce9125518fbbc4803ace959dbdb6.js.map