import { TextAsset, assetManager } from "cc"; import { GameServerAuthParams } from "../shared/types/GameServerAuthParams"; import { UserInfo } from "../shared/types/UserInfo"; import { GameServerListFileURL, GameServerURLs } from "./FrontConfig"; import { WebsocketClient } from "./WebsocketClient"; export class NetGameServer extends WebsocketClient { private _playerNum: number = 0; private _roomId: string; private _roomDisplayName: string; private _currentUser: UserInfo; private static _inst: NetGameServer = null; public static get inst(): NetGameServer { if (!this._inst) { this._inst = new NetGameServer(); } return this._inst; } public async getGameServerList(): Promise> { /** * @en if defined, load server list from remote file. * @zh 如果配置了此值,则优先从此值加载 */ if(GameServerListFileURL){ return new Promise((resolve, reject) => { assetManager.loadRemote(GameServerListFileURL,TextAsset, (err,textAsset:TextAsset)=>{ let serverUrls = GameServerURLs; try { serverUrls = JSON.parse(textAsset.text); } catch (error) { } resolve(serverUrls); }); }); } return GameServerURLs; } public authParams: GameServerAuthParams; public async connectToRoomServer(params: GameServerAuthParams) { this.authParams = params; console.log('connect to room server', params); if (this._serverUrl != params.serverUrl) { this.createConnection([params.serverUrl]); } return true; } public async joinRoomServer(uid:string) { let retJoin = await this.conn.callApi('EnterRoom', { token: this.authParams.token, uid: uid, time: this.authParams.time, roomId: this.authParams.roomId, }); return retJoin; } public get currentUser(): UserInfo { return this._currentUser; } public set playerNum(v: number) { this._playerNum = v; } public get playerNum() { return this._playerNum; } public get gameServerUrl() { return this.authParams.serverUrl; } public set roomId(v: string) { this._roomId = v; } public get roomId(): string { return this._roomId; } public set subRoomDisplayName(v: string) { this._roomDisplayName = v; } }