GameServerModel.ts 3.9 KB

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