msg.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import WebSocket from "ws";
  2. const logger = require("./log");
  3. export default class Msg {
  4. public sendBufLen = 0;
  5. public sendBuf = new ArrayBuffer(512);
  6. public sendBufDV = new DataView(this.sendBuf, 0);
  7. public websocket;
  8. public isConnect: Boolean;
  9. public isConnecting: boolean;
  10. public websocketM;
  11. public isConnectM: Boolean;
  12. public isConnectingM: Boolean;
  13. public isInitiative: boolean = false;
  14. public handler;
  15. public account;
  16. onOpen(evt) {
  17. this.isConnecting = false;
  18. if (this.websocket.readyState == 1 && !this.isConnect) {
  19. this.isConnect = true;
  20. console.log("连接成功");
  21. } else {
  22. console.log("连接服务器失败(" + this.websocket.readyState + ")");
  23. }
  24. }
  25. onClose(evt) {
  26. this.isConnect = false;
  27. this.isConnecting = false;
  28. if (this.websocket) {
  29. this.websocket.close();
  30. }
  31. if (this.isInitiative) this.isInitiative = false;
  32. if (this.isConnectM) this.websocketM.close();
  33. }
  34. onMessage(evt) {
  35. this.isConnect = false;
  36. this.isConnecting = false;
  37. // console.log("receive message ",evt.data)
  38. }
  39. onError(evt) {
  40. this.isConnect = false;
  41. this.isConnecting = false;
  42. if (this.websocket) {
  43. this.websocket.close();
  44. }
  45. if (this.isConnectM) this.websocketM.close();
  46. // PlatformManager.onLoginError();
  47. // LoadingPanel.instance.tip = "连接服务器失败(" + Msg.websocket.readyState + ")";
  48. }
  49. connect(url, inputAccount) {
  50. console.log("开始连接 url ============== ", url);
  51. if (this.isConnect) {
  52. console.log("msg connect 连接已存在");
  53. } else if (this.isConnecting) {
  54. console.log("msg connect 正在连接中");
  55. } else {
  56. console.log("msg connect 开始连接");
  57. const _that = this;
  58. this.isConnect = false;
  59. this.isConnecting = true;
  60. this.websocket = new WebSocket(url);
  61. this.websocket.binaryType = "arraybuffer";
  62. this.websocket.onopen = function (evt) {
  63. _that.onOpen(evt);
  64. };
  65. this.websocket.onclose = function (evt) {
  66. _that.onClose(evt);
  67. };
  68. this.websocket.onmessage = function (evt) {
  69. _that.onMessage(evt);
  70. };
  71. this.websocket.onerror = function (evt) {
  72. _that.onError(evt);
  73. };
  74. this.account = inputAccount;
  75. }
  76. }
  77. connectM(url) {
  78. if (this.isConnectM) {
  79. console.log("msg connectM 连接已存在");
  80. } else if (this.isConnectingM) {
  81. console.log("msg connectM 正在连接中");
  82. } else {
  83. const _that = this;
  84. this.isConnectM = false;
  85. this.isConnectingM = true;
  86. this.websocketM = new WebSocket(url);
  87. this.websocketM.binaryType = "arraybuffer";
  88. this.websocketM.onopen = function (evt) {
  89. _that.onOpenM(evt);
  90. };
  91. this.websocketM.onclose = function (evt) {
  92. _that.onCloseM(evt);
  93. };
  94. this.websocketM.onmessage = function (evt) {
  95. _that.handler.onMessageM(evt);
  96. };
  97. this.websocketM.onerror = function (evt) {
  98. _that.onErrorM(evt);
  99. };
  100. }
  101. }
  102. onOpenM(evt) {
  103. this.isConnectingM = false;
  104. if (this.websocketM.readyState == 1 && !this.isConnectM) {
  105. this.isConnectM = true;
  106. // Msg.CG_MIDDLE_LOGIN(account);
  107. } else {
  108. //trace("sopcket middle err:", websocketM.readyState);
  109. }
  110. }
  111. onCloseM(evt) {
  112. this.isConnectM = false;
  113. this.isConnectingM = false;
  114. console.log("onCloseM>>>>>>>>>>>>>>>>>>>>>>>>>>");
  115. if (this.websocketM) {
  116. this.websocketM.close();
  117. }
  118. }
  119. onErrorM(evt) {
  120. this.isConnectM = false;
  121. this.isConnectM = false;
  122. this.isConnectingM = false;
  123. console.log("onErrorM>>>>>>>>>>>>>>>>>>>>>>>>>>");
  124. if (this.websocketM) {
  125. this.websocketM.close();
  126. }
  127. }
  128. writeDouble(n) {
  129. this.sendBufDV.setFloat64(this.sendBufLen, n);
  130. this.sendBufLen = this.sendBufLen + 8;
  131. }
  132. writeInt(n) {
  133. this.sendBufDV.setInt32(this.sendBufLen, n);
  134. this.sendBufLen = this.sendBufLen + 4;
  135. }
  136. writeUint(n) {
  137. this.sendBufDV.setUint32(this.sendBufLen, n);
  138. this.sendBufLen = this.sendBufLen + 4;
  139. }
  140. writeShort(n) {
  141. this.sendBufDV.setInt16(this.sendBufLen, n);
  142. this.sendBufLen = this.sendBufLen + 2;
  143. }
  144. writeByte(n) {
  145. this.sendBufDV.setInt8(this.sendBufLen, n);
  146. this.sendBufLen = this.sendBufLen + 1;
  147. }
  148. writeString(str) {
  149. // 参数验证:确保 str 不为 undefined 或 null
  150. if (str === undefined || str === null) {
  151. console.error("writeString 接收到 undefined 或 null 参数:", str);
  152. str = ""; // 使用空字符串作为默认值
  153. }
  154. var oldPos = this.sendBufLen;
  155. this.writeShort(0);
  156. var totalByte = 0;
  157. var strLen = str.length;
  158. for (var i = 0; i < strLen; ++i) {
  159. var t = str.charCodeAt(i);
  160. if (t < 128) {
  161. totalByte = totalByte + 1;
  162. this.writeByte(t);
  163. } else if (t < 2048) {
  164. totalByte = totalByte + 2;
  165. this.writeByte((t >> 6) | 192);
  166. this.writeByte((t & 63) | 128);
  167. } else if (t < 65536) {
  168. totalByte = totalByte + 3;
  169. this.writeByte((t >> 12) | 224);
  170. this.writeByte(((t >> 6) & 63) | 128);
  171. this.writeByte((t & 63) | 128);
  172. } else {
  173. throw "error";
  174. }
  175. }
  176. this.sendBufDV.setInt16(oldPos, totalByte);
  177. }
  178. send(packetID) {
  179. var ab = new Uint8Array(this.sendBufLen);
  180. for (var i = 0; i < this.sendBufLen; ++i) {
  181. ab[i] = this.sendBufDV.getInt8(i);
  182. }
  183. this.websocket.send(ab.buffer);
  184. }
  185. close() {
  186. if (this.websocket) {
  187. this.websocket.close();
  188. }
  189. if (this.websocketM) {
  190. this.websocketM.close();
  191. }
  192. this.isConnect = false;
  193. this.isConnecting = false;
  194. this.isConnectM = false;
  195. this.isConnectingM = false;
  196. }
  197. CG_ASK_LOGIN(
  198. account: string,
  199. timestamp: number,
  200. authkey: string,
  201. lang: string,
  202. region: string,
  203. ip: string,
  204. params: string,
  205. server_id: number
  206. ) {
  207. logger.info("发送发货消息开始", {params:params, readyState: this.websocket.readyState, isConnect: this.isConnect });
  208. logger.info("发送登录消息", {readyState: this.websocket.readyState,isConnect: this.isConnect});
  209. if (this.isConnect != true || this.websocket.readyState != 1) return false;
  210. // 参数验证和默认值处理
  211. const safeAccount = account || "";
  212. const safeAuthkey = authkey || "";
  213. const safeLang = lang || "cn";
  214. const safeRegion = region || "CN";
  215. const safeIp = ip || "127.0.0.1";
  216. const safeParams = params || "{}";
  217. this.sendBufLen = 4;
  218. this.writeString(safeAccount);
  219. this.writeInt(timestamp);
  220. this.writeString(safeAuthkey);
  221. this.writeString(safeLang);
  222. this.writeString(safeRegion);
  223. this.writeString(safeIp);
  224. this.writeString(safeParams);
  225. this.writeInt(server_id);
  226. this.sendBufDV.setInt16(2, 62);
  227. this.sendBufDV.setInt16(0, this.sendBufLen - 4);
  228. this.send(62);
  229. logger.info("发送发货消息结束", {params:params, readyState: this.websocket.readyState, isConnect: this.isConnect });
  230. this.close(); // 主动断开连接
  231. return true;
  232. }
  233. CG_TEST_PROTO(account: string, param: string, server_id = 1) {
  234. console.log("发送CDK道具消息", this.websocket.readyState, this.isConnect,param,server_id,account);
  235. if (this.isConnect != true || this.websocket.readyState != 1) return false;
  236. this.sendBufLen = 4;
  237. this.writeString(account);
  238. this.writeString(param);
  239. this.writeInt(server_id);
  240. this.sendBufDV.setInt16(2, 1259);
  241. this.sendBufDV.setInt16(0, this.sendBufLen - 4);
  242. this.send(1259);
  243. console.log("发送完了断开连接");
  244. this.close(); // 主动断开连接
  245. return true;
  246. }
  247. CG_SET_BAN(banInfo: string) {
  248. console.log("发送封禁消息", this.websocket.readyState, this.isConnect);
  249. if (this.isConnect != true || this.websocket.readyState != 1) return false;
  250. this.sendBufLen = 4;
  251. this.writeString(banInfo);
  252. this.sendBufDV.setInt16(2, 1490);
  253. this.sendBufDV.setInt16(0, this.sendBufLen - 4);
  254. this.send(1490);
  255. console.log("发送完了断开连接");
  256. this.close(); // 主动断开连接
  257. return true;
  258. }
  259. CG_CHAT_NEW_BAN(
  260. uuid: string,
  261. type: number,
  262. time: number,
  263. reason: string,
  264. ) {
  265. logger.info("发送禁言消息开始", {uuid:uuid,type:type,time:time,reason:reason, readyState: this.websocket.readyState, isConnect: this.isConnect });
  266. // console.log("发送登录消息", this.websocket.readyState, this.isConnect);
  267. if (this.isConnect != true || this.websocket.readyState != 1) return false;
  268. this.sendBufLen = 4;
  269. this.writeString(uuid);
  270. this.writeInt(type);
  271. this.writeInt(time);
  272. this.writeString(reason);
  273. this.sendBufDV.setInt16(2, 62);
  274. this.sendBufDV.setInt16(0, this.sendBufLen - 4);
  275. this.send(62);
  276. this.close(); // 主动断开连接
  277. return true;
  278. }
  279. }