Files
rougelike-demo/client/assets/scripts/App/Msg/MsgExample.ts

84 lines
2.2 KiB
TypeScript
Raw Normal View History

import { initMessagePairs } from './MessagePairInit';
import { MsgManager } from './MsgManager';
/**
* 使
*/
export class MsgExample {
/**
*
*
*/
static init(): void {
// 初始化消息对注册表
initMessagePairs();
console.log('[MsgExample] Message system initialized');
}
/**
*
*/
static async loginExample(): Promise<void> {
const msgMgr = MsgManager.getInstance();
try {
const response = await msgMgr.login({
playerId: 'player123',
playerName: 'TestPlayer'
});
if (response && response.success) {
console.log('登录成功:', response.player);
} else {
console.log('登录失败:', response?.message);
}
} catch (error) {
console.error('登录请求异常:', error);
}
}
/**
*
*/
static async moveExample(): Promise<void> {
const msgMgr = MsgManager.getInstance();
try {
const response = await msgMgr.move({
x: 100,
y: 200
});
if (response && response.success) {
console.log('移动成功:', response.position);
} else {
console.log('移动失败:', response?.message);
}
} catch (error) {
console.error('移动请求异常:', error);
}
}
/**
*
*/
static async sendMessageExample(): Promise<void> {
const msgMgr = MsgManager.getInstance();
try {
const response = await msgMgr.send({
content: 'Hello, World!'
});
if (response) {
console.log('消息发送成功:', response.time);
} else {
console.log('消息发送失败');
}
} catch (error) {
console.error('发送消息异常:', error);
}
}
}