NetGameServer.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { TextAsset, assetManager } from "cc";
  2. import { GameServerAuthParams } from "../shared/types/GameServerAuthParams";
  3. import { GameServerListFileURL, GameServerURLs } from "./FrontConfig";
  4. import { WebsocketClient } from "./WebsocketClient";
  5. import { UserLocalCache } from "./UserLocalCache";
  6. import { ApiReturn, HttpClient } from "tsrpc-browser";
  7. import { ServiceType } from "../shared/protocols/serviceProto_public";
  8. import { NetUtil } from "./NetUtil";
  9. import { LOBBY_USE_HTTP } from "../shared/configs/Constants";
  10. import { ResLogin } from "../shared/protocols/public/login/PtlLogin";
  11. export class NetLobbyServer extends WebsocketClient {
  12. public authParams: ResLogin;
  13. constructor() {
  14. super();
  15. console.log('Lobby Connection created as Websocket');
  16. }
  17. }
  18. export class HttpLobbyServer {
  19. private _globalErrorFilters = {};
  20. public addErrorFilter(error: string, cb: Function, target?: any) {
  21. this._globalErrorFilters[error] = { cb: cb, target: target };
  22. }
  23. private _http: HttpClient<ServiceType>;
  24. private _serverUrl = '';
  25. public authParams: ResLogin;
  26. public get type() {
  27. return 'http';
  28. }
  29. public get serverUrl() {
  30. return this._serverUrl;
  31. }
  32. constructor(){
  33. console.log('Lobby Connection created as HTTP');
  34. }
  35. createConnection(serverUrls: Readonly<string[]>) {
  36. let index = Math.floor(serverUrls.length * Math.random());
  37. let serverUrl = serverUrls[index];
  38. this._serverUrl = serverUrl;
  39. this._http = NetUtil.createHttpClient(this._serverUrl);
  40. }
  41. public async callApi<T extends string & keyof ServiceType['api']>(apiName: T, req: ServiceType['api'][T]['req'], options?: any): Promise<ApiReturn<ServiceType['api'][T]['res']>> {
  42. if(this.authParams){
  43. req['token'] = this.authParams.token;
  44. }
  45. let ret = await this._http?.callApi(apiName, req, options);
  46. if(!ret.isSucc){
  47. let filter = this._globalErrorFilters[ret.err.message];
  48. if(filter){
  49. filter.cb.call(filter.target, ret.err);
  50. }
  51. }
  52. return ret;
  53. }
  54. }
  55. export class NetGameServer extends WebsocketClient {
  56. public authParams: GameServerAuthParams;
  57. public async connectToRoomServer(params: GameServerAuthParams) {
  58. this.authParams = params;
  59. this.createConnection([params.serverUrl]);
  60. return true;
  61. }
  62. public async joinRoomServer(uid: string) {
  63. let retJoin = await this.conn.callApi('game/AuthClient', {
  64. sign: this.authParams.token,
  65. uid: uid,
  66. time: this.authParams.time,
  67. roomId: this.authParams.roomId,
  68. gameType: this.authParams.gameType,
  69. roleName: UserLocalCache.inst.getRoleName(uid),
  70. });
  71. return retJoin;
  72. }
  73. }
  74. export const loginNet = new HttpLobbyServer();
  75. export const lobbyNet = LOBBY_USE_HTTP ? new HttpLobbyServer() : new NetLobbyServer();
  76. export const gameNet = new NetGameServer();