Files
youle_cocos/cocoscreator_projects/scripts/login-live.ts
T

77 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 真机联调脚本(手动跑,非单测):复用框架代码,对真实配置服 + 本地游戏服跑 player_login 往返。
// 运行:node --import tsx scripts/login-live.ts (cwd: cocoscreator_projects)
import WebSocket from 'ws';
import { resolveBootstrap } from '../YouleNexus/assets/framework/config/bootstrap.ts';
import { HttpConfigFetcher } from '../YouleNexus/assets/framework/config/remote-config-fetcher.ts';
import { resolveRuntimeMode } from '../YouleNexus/assets/framework/config/runtime-mode.ts';
import { NetClient } from '../YouleNexus/assets/framework/net/net-client.ts';
import { parseLoginResponse } from '../YouleNexus/assets/framework/protocol/login.ts';
import type { Transport } from '../YouleNexus/assets/framework/net/transport.ts';
/** Node 侧 Transport:用 ws 包实现框架的 Transport 接口(等价生产的 CocosWebSocketTransport)。 */
class WsTransport implements Transport {
private ws: WebSocket | null = null;
private openCb?: () => void;
private msgCb?: (f: string) => void;
private closeCb?: () => void;
connect(url: string): void {
const ws = new WebSocket(url);
this.ws = ws;
ws.on('open', () => this.openCb?.());
ws.on('message', (data: any) => this.msgCb?.(data.toString()));
ws.on('close', () => this.closeCb?.());
ws.on('error', (e: any) => console.log('[ws error]', e?.message ?? e));
}
send(frame: string): void { this.ws?.send(frame); }
close(): void { try { this.ws?.close(); } catch { /* ignore */ } }
onOpen(cb: () => void): void { this.openCb = cb; }
onMessage(cb: (f: string) => void): void { this.msgCb = cb; }
onClose(cb: () => void): void { this.closeCb = cb; }
}
async function main(): Promise<void> {
const mode = resolveRuntimeMode(true, 'debug'); // 强制 debug
console.log('== 联调开始 (mode=' + mode + ', profile=staging) ==');
const result = await resolveBootstrap({
mode,
win: {},
search: '?profile=staging',
fetcher: new HttpConfigFetcher(),
isNative: false,
});
console.log('profile :', result.profile.name);
console.log('servers :', result.servers, '(serverOverride 生效)');
console.log('identity :', result.identity);
console.log('account :', result.account);
const data = (result.rawConfig as any)?.data;
console.log('rawConfig :', data ? '顶层键=' + Object.keys(data).join(',') : null);
const net = new NetClient({
servers: result.servers,
transportFactory: () => new WsTransport(),
debug: true,
});
net.setIdentity({ ...result.identity, ...(result.account ?? {}) } as any);
net.on('open', () => console.log('[event] open(已连上本地服)'));
net.on('login', (loginData: any) => {
const parsed = parseLoginResponse(loginData);
console.log('[event] login → parseLoginResponse:', parsed);
console.log(parsed.ok ? `✅ 登录成功 playerid=${parsed.playerid} inRoom=${parsed.inRoom} hasBattle=${parsed.hasBattle}` : '❌ 登录失败 state≠0');
setTimeout(() => { net.stop(); process.exit(0); }, 500);
});
net.on('message', (m: any) => console.log('[event] message route=' + m.route + ' rpc=' + m.rpc));
net.on('slow', () => console.log('[event] slow(收包超时)'));
net.on('reconnecting', (s: any) => console.log('[event] reconnecting →', s));
net.on('serverSwitch', (s: any) => console.log('[event] serverSwitch →', s));
net.on('kicked', (d: any) => console.log('[event] kicked', d));
net.start();
setTimeout(() => { console.log('⏱ 15s 内未完成登录,结束。'); net.stop(); process.exit(2); }, 15000);
}
main().catch((e) => { console.error('❌ 联调异常:', e?.message ?? e); process.exit(1); });