update
This commit is contained in:
parent
1ade200675
commit
3b17c31857
@ -1,10 +1,10 @@
|
|||||||
import { EventEmitter } from "stream";
|
import { EventEmitter } from "stream"
|
||||||
import WebSocket, { WebSocketServer } from "ws";
|
import WebSocket, { WebSocketServer } from "ws"
|
||||||
import { ApiMsgEnum } from "../Common";
|
import { ApiMsgEnum, IModel } from "../Common"
|
||||||
import { Connection, ConnectionEventEnum } from "./Connection";
|
import { Connection, ConnectionEventEnum } from "./Connection"
|
||||||
|
|
||||||
export interface IMyServerOptions {
|
export interface IMyServerOptions {
|
||||||
port: number;
|
port: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum MyServerEventEnum {
|
export enum MyServerEventEnum {
|
||||||
@ -13,51 +13,51 @@ export enum MyServerEventEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class MyServer extends EventEmitter {
|
export class MyServer extends EventEmitter {
|
||||||
wss?: WebSocketServer;
|
wss?: WebSocketServer
|
||||||
port: number;
|
port: number
|
||||||
connections: Set<Connection> = new Set();
|
connections: Set<Connection> = new Set()
|
||||||
apiMap: Map<ApiMsgEnum, Function> = new Map();
|
apiMap: Map<ApiMsgEnum, Function> = new Map()
|
||||||
|
|
||||||
constructor({ port = 8080 }: Partial<IMyServerOptions>) {
|
constructor({ port = 8080 }: Partial<IMyServerOptions>) {
|
||||||
super();
|
super()
|
||||||
this.port = port;
|
this.port = port
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.wss = new WebSocketServer({ port: this.port });
|
this.wss = new WebSocketServer({ port: this.port })
|
||||||
this.wss.on("connection", this.handleConnect.bind(this));
|
this.wss.on("connection", this.handleConnect.bind(this))
|
||||||
|
|
||||||
this.wss.on("error", (e) => {
|
this.wss.on("error", (e) => {
|
||||||
reject(e);
|
reject(e)
|
||||||
});
|
})
|
||||||
|
|
||||||
this.wss.on("close", () => {
|
this.wss.on("close", () => {
|
||||||
console.log("MyServer 服务关闭");
|
console.log("MyServer 服务关闭")
|
||||||
});
|
})
|
||||||
|
|
||||||
this.wss.on("listening", () => {
|
this.wss.on("listening", () => {
|
||||||
resolve(true);
|
resolve(true)
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handleConnect(ws: WebSocket) {
|
handleConnect(ws: WebSocket) {
|
||||||
//初始化
|
//初始化
|
||||||
const connection = new Connection(this, ws);
|
const connection = new Connection(this, ws)
|
||||||
|
|
||||||
//向外告知有人来了
|
//向外告知有人来了
|
||||||
this.connections.add(connection);
|
this.connections.add(connection)
|
||||||
this.emit(MyServerEventEnum.Connect, connection);
|
this.emit(MyServerEventEnum.Connect, connection)
|
||||||
|
|
||||||
//向外告知有人走了
|
//向外告知有人走了
|
||||||
connection.on(ConnectionEventEnum.Close, (code: number, reason: string) => {
|
connection.on(ConnectionEventEnum.Close, (code: number, reason: string) => {
|
||||||
this.connections.delete(connection);
|
this.connections.delete(connection)
|
||||||
this.emit(MyServerEventEnum.DisConnect, connection, code, reason);
|
this.emit(MyServerEventEnum.DisConnect, connection, code, reason)
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
setApi(apiName: ApiMsgEnum, cb: Function) {
|
setApi<T extends keyof IModel["api"]>(name: T, cb: (connection: Connection, args: IModel["api"][T]["res"]) => void) {
|
||||||
this.apiMap.set(apiName, cb);
|
this.apiMap.set(name, cb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user