This commit is contained in:
sli97 2022-12-13 13:44:53 +08:00
parent 6d37ab0468
commit 99d68b0ee7
2 changed files with 112 additions and 107 deletions

View File

@ -1,83 +1,82 @@
import WebSocket from "ws";
import { EventEmitter } from "stream";
import { MyServer } from "./MyServer";
import { getTime, buffer2ArrayBuffer } from "../Utils";
import { ApiMsgEnum, IModel } from "../Common";
import { binaryEncode, binaryDecode } from "../Common/Binary";
import WebSocket from "ws"
import { EventEmitter } from "stream"
import { MyServer } from "./MyServer"
import { getTime, buffer2ArrayBuffer } from "../Utils"
import { ApiMsgEnum, IModel } from "../Common"
import { binaryEncode, binaryDecode } from "../Common/Binary"
export enum ConnectionEventEnum {
Close = "Close",
}
interface IItem {
cb: Function;
ctx: unknown;
cb: Function
ctx: unknown
}
export class Connection extends EventEmitter {
server: MyServer;
ws: WebSocket;
msgMap: Map<ApiMsgEnum, Array<IItem>> = new Map();
playerId?: number;
server: MyServer
ws: WebSocket
msgMap: Map<ApiMsgEnum, Array<IItem>> = new Map()
constructor(server: MyServer, ws: WebSocket) {
super();
super()
this.server = server;
this.ws = ws;
this.server = server
this.ws = ws
this.ws.on("close", (code: number, reason: Buffer) => {
this.emit(ConnectionEventEnum.Close, code, reason.toString());
});
this.emit(ConnectionEventEnum.Close, code, reason.toString())
})
this.ws.on("message", (buffer: Buffer) => {
// const str = buffer.toString()
try {
const json = binaryDecode(buffer2ArrayBuffer(buffer));
const { name, data } = json;
const json = binaryDecode(buffer2ArrayBuffer(buffer))
const { name, data } = json
// console.log(`${getTime()}接收|字节数${buffer.length}|${this.playerId || -1}|${str}`)
console.log(`${getTime()}接收|字节数${buffer.length}|${this.playerId || -1}|${JSON.stringify(json)}`);
console.log(`${getTime()}接收|字节数${buffer.length}|${this.playerId || -1}|${JSON.stringify(json)}`)
if (this.server.apiMap.has(name)) {
try {
const cb = this.server.apiMap.get(name);
const res = cb.call(null, this, data);
const cb = this.server.apiMap.get(name)
const res = cb.call(null, this, data)
this.sendMsg(name, {
success: true,
res,
});
})
} catch (error) {
this.sendMsg(name, {
success: false,
error: (error as Error)?.message,
});
})
}
} else {
try {
if (this.msgMap.has(name)) {
this.msgMap.get(name).forEach(({ cb, ctx }) => cb.call(ctx, this, data));
this.msgMap.get(name).forEach(({ cb, ctx }) => cb.call(ctx, this, data))
}
} catch (error) {
console.log(error);
console.log(error)
}
}
} catch (error) {
console.log(`解析失败不是合法的JSON格式`, error);
console.log(`解析失败不是合法的JSON格式`, error)
}
});
})
}
listenMsg<T extends keyof IModel["msg"]>(name: T, cb: (connection: Connection, arg: IModel["msg"][T]) => void, ctx: unknown) {
if (this.msgMap.has(name)) {
this.msgMap.get(name).push({ cb, ctx });
this.msgMap.get(name).push({ cb, ctx })
} else {
this.msgMap.set(name, [{ cb, ctx }]);
this.msgMap.set(name, [{ cb, ctx }])
}
}
unlistenMsg<T extends keyof IModel["msg"]>(name: T, cb: (connection: Connection, arg: IModel["msg"][T]) => void, ctx: unknown) {
if (this.msgMap.has(name)) {
const items = this.msgMap.get(name);
const index = items.findIndex((i) => cb === i.cb && i.ctx === ctx);
index > -1 && items.splice(index, 1);
const items = this.msgMap.get(name)
const index = items.findIndex((i) => cb === i.cb && i.ctx === ctx)
index > -1 && items.splice(index, 1)
}
}
@ -85,14 +84,14 @@ export class Connection extends EventEmitter {
const msg = JSON.stringify({
name,
data,
});
const view = binaryEncode(name, data);
const buffer = Buffer.from(view.buffer);
})
const view = binaryEncode(name, data)
const buffer = Buffer.from(view.buffer)
console.log(
`${getTime()}发送|字节数${buffer.length}|${this.playerId || -1}|内存${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(
2
)}MB|${msg}`
);
this.ws.send(buffer);
)
this.ws.send(buffer)
}
}

View File

@ -1,7 +1,7 @@
import { Connection, MyServer, MyServerEventEnum } from "./Core";
import PlayerManager from "./Biz/PlayerManager";
import RoomManager from "./Biz/RoomManager";
import { getTime, symlinkCommon } from "./Utils";
import { Connection, MyServer, MyServerEventEnum } from "./Core"
import PlayerManager from "./Biz/PlayerManager"
import RoomManager from "./Biz/RoomManager"
import { getTime, symlinkCommon } from "./Utils"
import {
ApiMsgEnum,
IApiGameEndReq,
@ -21,145 +21,151 @@ import {
IApiRoomListReq,
IApiRoomListRes,
IModel,
} from "./Common";
} from "./Common"
const server = new MyServer({ port: 8888 });
declare module "./Core" {
interface Connection {
playerId?: number
}
}
const server = new MyServer({ port: 8888 })
// event
server.on(MyServerEventEnum.Connect, (connection: Connection) => {
console.log(`${getTime()}来人|人数|${server.connections.size}`);
});
console.log(`${getTime()}来人|人数|${server.connections.size}`)
})
server.on(MyServerEventEnum.DisConnect, (connection: Connection) => {
console.log(`${getTime()}走人|人数|${server.connections.size}`);
console.log(`${getTime()}走人|人数|${server.connections.size}`)
if (connection.playerId) {
PlayerManager.Instance.removePlayer(connection.playerId);
PlayerManager.Instance.removePlayer(connection.playerId)
}
});
})
// api
server.setApi(ApiMsgEnum.ApiPlayerList, (connection: Connection, data: IApiPlayerListReq): IApiPlayerListRes => {
return { list: PlayerManager.Instance.getPlayersView() };
});
return { list: PlayerManager.Instance.getPlayersView() }
})
server.setApi(ApiMsgEnum.ApiPlayerJoin, (connection: Connection, { nickname }: IApiPlayerJoinReq): IApiPlayerJoinRes => {
const player = PlayerManager.Instance.createPlayer({ connection, nickname });
PlayerManager.Instance.syncPlayers();
const player = PlayerManager.Instance.createPlayer({ connection, nickname })
PlayerManager.Instance.syncPlayers()
return {
player: PlayerManager.Instance.getPlayerView(player),
};
});
}
})
server.setApi(ApiMsgEnum.ApiRoomList, (connection: Connection, data: IApiRoomListReq): IApiRoomListRes => {
return { list: RoomManager.Instance.getRoomsView() };
});
return { list: RoomManager.Instance.getRoomsView() }
})
server.setApi(ApiMsgEnum.ApiRoomCreate, (connection: Connection, data: IApiRoomCreateReq): IApiRoomCreateRes => {
if (connection.playerId) {
const room = RoomManager.Instance.joinRoom(RoomManager.Instance.createRoom().id, connection.playerId);
const room = RoomManager.Instance.joinRoom(RoomManager.Instance.createRoom().id, connection.playerId)
if (room) {
RoomManager.Instance.syncRooms();
PlayerManager.Instance.syncPlayers();
RoomManager.Instance.syncRooms()
PlayerManager.Instance.syncPlayers()
return {
room: RoomManager.Instance.getRoomView(room),
};
} else {
throw new Error("ApiRoomCreate room不存在");
}
} else {
throw new Error("ApiRoomCreate 玩家未登录");
throw new Error("ApiRoomCreate room不存在")
}
});
} else {
throw new Error("ApiRoomCreate 玩家未登录")
}
})
server.setApi(ApiMsgEnum.ApiRoomJoin, (connection: Connection, data: IApiRoomJoinReq): IApiRoomJoinRes => {
if (connection.playerId) {
const room = RoomManager.Instance.joinRoom(data.rid, connection.playerId);
const room = RoomManager.Instance.joinRoom(data.rid, connection.playerId)
if (room) {
RoomManager.Instance.syncRooms();
PlayerManager.Instance.syncPlayers();
RoomManager.Instance.syncRoom(room.id);
RoomManager.Instance.syncRooms()
PlayerManager.Instance.syncPlayers()
RoomManager.Instance.syncRoom(room.id)
return {
room: RoomManager.Instance.getRoomView(room),
};
} else {
throw new Error("ApiRoomJoin room不存在");
}
} else {
throw new Error("ApiRoomJoin 玩家未登录");
throw new Error("ApiRoomJoin room不存在")
}
});
} else {
throw new Error("ApiRoomJoin 玩家未登录")
}
})
server.setApi(ApiMsgEnum.ApiRoomLeave, (connection: Connection, data: IApiRoomLeaveReq): IApiRoomLeaveRes => {
if (connection.playerId) {
const player = PlayerManager.Instance.getPlayerById(connection.playerId);
const player = PlayerManager.Instance.getPlayerById(connection.playerId)
if (player) {
const rid = player.rid;
const rid = player.rid
if (rid) {
RoomManager.Instance.leaveRoom(rid, player.id);
PlayerManager.Instance.syncPlayers();
RoomManager.Instance.syncRooms();
RoomManager.Instance.syncRoom(rid);
return {};
RoomManager.Instance.leaveRoom(rid, player.id)
PlayerManager.Instance.syncPlayers()
RoomManager.Instance.syncRooms()
RoomManager.Instance.syncRoom(rid)
return {}
} else {
throw new Error("ApiRoomLeave 玩家不在房间");
throw new Error("ApiRoomLeave 玩家不在房间")
}
} else {
throw new Error("ApiRoomLeave 玩家不存在");
throw new Error("ApiRoomLeave 玩家不存在")
}
} else {
throw new Error("ApiRoomLeave 玩家未登录");
throw new Error("ApiRoomLeave 玩家未登录")
}
});
})
server.setApi(ApiMsgEnum.ApiGameStart, (connection: Connection, data: IApiGameStartReq): IApiGameStartRes => {
if (connection.playerId) {
const player = PlayerManager.Instance.getPlayerById(connection.playerId);
const player = PlayerManager.Instance.getPlayerById(connection.playerId)
if (player) {
const rid = player.rid;
const rid = player.rid
if (rid) {
RoomManager.Instance.startRoom(rid);
PlayerManager.Instance.syncPlayers();
RoomManager.Instance.syncRooms();
return {};
RoomManager.Instance.startRoom(rid)
PlayerManager.Instance.syncPlayers()
RoomManager.Instance.syncRooms()
return {}
} else {
throw new Error("ApiRoomLeave 玩家不在房间");
throw new Error("ApiRoomLeave 玩家不在房间")
}
} else {
throw new Error("ApiRoomLeave 玩家不存在");
throw new Error("ApiRoomLeave 玩家不存在")
}
} else {
throw new Error("ApiRoomLeave 玩家未登录");
throw new Error("ApiRoomLeave 玩家未登录")
}
});
})
server.setApi(ApiMsgEnum.ApiGameEnd, (connection: Connection, data: IApiGameEndReq): IApiGameEndRes => {
if (connection.playerId) {
const player = PlayerManager.Instance.getPlayerById(connection.playerId);
const player = PlayerManager.Instance.getPlayerById(connection.playerId)
if (player) {
const rid = player.rid;
const rid = player.rid
if (rid) {
RoomManager.Instance.closeRoom(rid);
PlayerManager.Instance.syncPlayers();
RoomManager.Instance.syncRooms();
return {};
RoomManager.Instance.closeRoom(rid)
PlayerManager.Instance.syncPlayers()
RoomManager.Instance.syncRooms()
return {}
} else {
throw new Error("ApiGameEnd 玩家不在房间");
throw new Error("ApiGameEnd 玩家不在房间")
}
} else {
throw new Error("ApiGameEnd 玩家不存在");
throw new Error("ApiGameEnd 玩家不存在")
}
} else {
throw new Error("ApiGameEnd 玩家未登录");
throw new Error("ApiGameEnd 玩家未登录")
}
});
})
// start!!
server
.start()
.then(() => {
symlinkCommon();
console.log("服务启动!");
symlinkCommon()
console.log("服务启动!")
})
.catch((e) => {
console.log("服务异常", e);
});
console.log("服务异常", e)
})