ad38d5d222a7c24988d3142d17b9d7c59ad3eb64.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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) {
  73. if (channelId === void 0) {
  74. channelId = 0;
  75. }
  76. this._channels[channelId] = node;
  77. }
  78. /** 移除Node */
  79. removeNetNode(channelId) {
  80. delete this._channels[channelId];
  81. }
  82. /**
  83. * 网络节点连接服务器
  84. * @param options 连接参数
  85. * @param channelId 通道编号
  86. * @example
  87. var options = {
  88. url: 'ws://127.0.0.1:3000',
  89. autoReconnect: 0 // -1 永久重连,0不自动重连,其他正整数为自动重试次数
  90. }
  91. NetManager.getInstance().connect(options, NetChannelType.Game);
  92. */
  93. connect(options, channelId) {
  94. if (channelId === void 0) {
  95. channelId = 0;
  96. }
  97. if (this._channels[channelId]) {
  98. return this._channels[channelId].connect(options);
  99. }
  100. return false;
  101. }
  102. /** 节点连接发送数据*/
  103. send(buf, force, channelId) {
  104. if (force === void 0) {
  105. force = false;
  106. }
  107. if (channelId === void 0) {
  108. channelId = 0;
  109. }
  110. var node = this._channels[channelId];
  111. if (node) {
  112. return node.send(buf, force);
  113. }
  114. return -1;
  115. }
  116. /**
  117. * 发起请求,并在在结果返回时调用指定好的回调函数
  118. * @param reqProtocol 请求协议
  119. * @param rspObject 回调对象
  120. * @param showTips 是否触发请求提示
  121. * @param force 是否强制发送
  122. * @param channelId 通道编号
  123. * @example
  124. let protocol: IRequestProtocol = {
  125. action: action,
  126. method: method,
  127. data: JSON.stringify(data),
  128. isCompress: this.isCompress,
  129. channelid: netConfig.channelid
  130. }
  131. return this.request(protocol, rspObject, showTips, force);
  132. */
  133. request(reqProtocol, rspObject, showTips, force, channelId) {
  134. if (showTips === void 0) {
  135. showTips = true;
  136. }
  137. if (force === void 0) {
  138. force = false;
  139. }
  140. if (channelId === void 0) {
  141. channelId = 0;
  142. }
  143. var node = this._channels[channelId];
  144. if (node) {
  145. node.request(reqProtocol, rspObject, showTips, force);
  146. }
  147. }
  148. /**
  149. * 同request功能一致,但在request之前会先判断队列中是否已有rspCmd,如有重复的则直接返回
  150. * @param reqProtocol 请求协议
  151. * @param rspObject 回调对象
  152. * @param showTips 是否触发请求提示
  153. * @param force 是否强制发送
  154. * @param channelId 通道编号
  155. * @example
  156. let protocol: IRequestProtocol = {
  157. action: action,
  158. method: method,
  159. data: JSON.stringify(data),
  160. isCompress: this.isCompress,
  161. channelid: netConfig.channelid
  162. }
  163. return this.request(protocol, rspObject, showTips, force);
  164. */
  165. requestUnique(reqProtocol, rspObject, showTips, force, channelId) {
  166. if (showTips === void 0) {
  167. showTips = true;
  168. }
  169. if (force === void 0) {
  170. force = false;
  171. }
  172. if (channelId === void 0) {
  173. channelId = 0;
  174. }
  175. var node = this._channels[channelId];
  176. if (node) {
  177. return node.requestUnique(reqProtocol, rspObject, showTips, force);
  178. }
  179. return false;
  180. }
  181. /**
  182. * 节点网络断开
  183. * @param code 关闭码
  184. * @param reason 关闭原因
  185. * @param channelId 通道编号
  186. * @example
  187. * NetManager.getInstance().close(undefined, undefined, NetChannelType.Game);
  188. */
  189. close(code, reason, channelId) {
  190. if (channelId === void 0) {
  191. channelId = 0;
  192. }
  193. if (this._channels[channelId]) {
  194. return this._channels[channelId].closeSocket(code, reason);
  195. }
  196. }
  197. });
  198. NetManager._instance = void 0;
  199. _cclegacy._RF.pop();
  200. _crd = false;
  201. }
  202. };
  203. });
  204. //# sourceMappingURL=ad38d5d222a7c24988d3142d17b9d7c59ad3eb64.js.map