GameServerModel.ts 3.6 KB

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