xy-server/game/network/AgentMgr.ts
2025-04-23 09:34:08 +08:00

81 lines
2.1 KiB
TypeScript

import SKLogger from "../gear/SKLogger";
import GameUtil from "../core/GameUtil";
import Agent from "./Agent";
import AgentBase from "./AgentBase";
let agent_seed_id = 1000;
export default class AgentMgr {
static shared = new AgentMgr();
io: any;
agent_list: any;
constructor() {
this.io = null;
this.agent_list = {};
}
addAgent(agent: Agent) {
agent.id = agent_seed_id;
this.agent_list[agent.id] = agent;
agent_seed_id++;
}
delAgent(agentId: number) {
let agent = this.agent_list[agentId];
if (agent == null) {
return;
}
delete this.agent_list[agentId];
}
getAgent(agentid: any) {
return this.agent_list[agentid];;
}
update(dt: number) {
for (const agent_id in this.agent_list) {
if (this.agent_list.hasOwnProperty(agent_id)) {
let agent = this.agent_list[agent_id];
agent.update(dt);
}
}
}
start() {
let websocket = require('ws').Server;
let wss = new websocket({ port: GameUtil.serverConf.socket_port });
wss.on("connection", (ws: any) => {
let agent = new Agent(ws);
agent.init();
this.addAgent(agent);
});
wss.on('error', (ws: any) => {
SKLogger.debug('error');
});
this.io = wss;
SKLogger.debug(`网关代理模块启动完毕,正在监听${GameUtil.serverConf.outer_ip}:${GameUtil.serverConf.socket_port}`);
}
// 关服
close() {
for (let agentid in this.agent_list) {
let agent: AgentBase = this.agent_list[agentid];
agent.close();
}
if (this.io) {
this.io.close();
}
}
getAgentByAccountid(accountid: any): any {
for (const agent_id in this.agent_list) {
if (this.agent_list.hasOwnProperty(agent_id)) {
const agent = this.agent_list[agent_id];
if (agent.accountid == accountid) {
return agent;
}
}
}
return null;
}
}