GameServerModel.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { eg } from "../frameWork/eg";
  2. import { GameServerConfig } from "../network/GameServerConfig";
  3. import { HttpClient as HttpClient_Browser, WsClient as WsClient_Browser } from 'tsrpc-browser';
  4. import { HttpClient as HttpClient_Miniapp, WsClient as WsClient_Miniapp } from 'tsrpc-miniapp';
  5. import { serviceProto as ServiceProtoRoom, ServiceType, ServiceType as ServiceTypeRoom } from "../shared/serviceProto";
  6. import IDataModel from "../data/model/IDataModel";
  7. import UIHelp, { DialogParams } from "../logic/ui/UIHelp";
  8. import GameController from "../GameController";
  9. import { I18n } from "../utils/I18nUtil";
  10. import EventMng from "../manager/EventMng";
  11. /** 游戏服 */
  12. export class GameServerModel extends IDataModel {
  13. /** 连接房间服务器 Websocket 客户端 */
  14. wscRoom: WsClient_Browser<ServiceTypeRoom> = null!;
  15. /** 重连次数 */
  16. reconnectCnt: number = 0;
  17. async Init() {
  18. this.wscRoom = eg.tspcNet.createWscRoom(GameServerConfig.GAME_SERVER);
  19. }
  20. /** 连接游戏服 */
  21. async Connect() {
  22. let resConnect = await this.wscRoom.connect();
  23. if (!resConnect.isSucc) {
  24. console.log('连接游戏服失败', resConnect.errMsg);
  25. return;
  26. }
  27. console.error('连接游戏服成功');
  28. this.PostDisconnectFlow();
  29. }
  30. /** 重连游戏服 */
  31. async ReConnect() {
  32. this.reconnectCnt++;
  33. let resConnect = await this.wscRoom.connect();
  34. if (!resConnect.isSucc) {
  35. console.log('连接游戏服失败', resConnect.errMsg);
  36. if (this.reconnectCnt >= GameServerConfig.reconnectMax) {
  37. CC_PREVIEW && console.error("重连服务器超过上限");
  38. let dialogParam: DialogParams = {
  39. content: I18n.getI18nText("http_overtime"),
  40. cbConfirm: () => {
  41. this.reconnectCnt = 0;
  42. GameController.clear();
  43. cc.game.restart();
  44. },
  45. txtConfirm: I18n.getI18nText("http_overtime_confirm_1"),
  46. onlyConfirm: true
  47. }
  48. UIHelp.ShowSystemDialog(dialogParam)
  49. return;
  50. }
  51. setTimeout(() => {
  52. this.ReConnect();
  53. }, 2000);
  54. return;
  55. }
  56. CC_PREVIEW && console.log("重连服务器成功");
  57. }
  58. /** 客户端与服务器断开事件 */
  59. private PostDisconnectFlow() {
  60. this.wscRoom.flows.postDisconnectFlow.push(v => {
  61. // 非客户端手动断开时处理(例:网络错误、服务器关闭)
  62. if (!v.isManual) {
  63. CC_PREVIEW && console.log("wss断线", v.reason);
  64. this.reconnectCnt = 0;
  65. // 等待 2 秒后自动重连
  66. this.ReConnect();
  67. }
  68. return v;
  69. });
  70. }
  71. /** 请求api */
  72. public async ReqApi<T extends string & keyof ServiceType['api']>(apiName: T, req: ServiceType['api'][T]['req']) {
  73. let ret = await this.wscRoom.callApi(apiName, req);
  74. if (!ret.isSucc) {
  75. console.error(ret.err);
  76. UIHelp.ShowTips(ret.err.message);
  77. }
  78. return ret;
  79. }
  80. /** ws发送消息 */
  81. public SendMsg<T extends string & keyof ServiceType['msg']>(msgName: T, msg: ServiceType['msg'][T]) {
  82. this.wscRoom.sendMsg(msgName, msg);
  83. }
  84. /** 监听消息 */
  85. public ListenMsg<T extends keyof ServiceType['msg']>(msgName: T | RegExp, callback: Function) {
  86. this.wscRoom.listenMsg(msgName, v => {
  87. callback(v);
  88. });
  89. }
  90. }