expose websockets (!321)

- Added 2 methods that expose the websocket server and each session's websocket

Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/321
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
This commit is contained in:
TheSparta 2024-05-04 18:02:20 +00:00 committed by chomp
parent d2e1624618
commit d3ac83e63b

View File

@ -32,14 +32,25 @@ export class WebSocketServer
protected httpConfig: IHttpConfig; protected httpConfig: IHttpConfig;
protected defaultNotification: INotification = { type: NotificationType.PING, eventId: "ping" }; protected defaultNotification: INotification = { type: NotificationType.PING, eventId: "ping" };
protected webSocketServer: WebSocket.Server;
protected webSockets: Record<string, WebSocket.WebSocket> = {}; protected webSockets: Record<string, WebSocket.WebSocket> = {};
protected websocketPingHandler = null; protected websocketPingHandler = null;
public getWebSocketServer(): WebSocket.Server
{
return this.webSocketServer;
}
public getSessionWebSocket(sessionID: string): WebSocket.WebSocket
{
return this.webSockets[sessionID];
}
public setupWebSocket(httpServer: http.Server): void public setupWebSocket(httpServer: http.Server): void
{ {
const webSocketServer = new WebSocket.Server({ server: httpServer }); this.webSocketServer = new WebSocket.Server({ server: httpServer });
webSocketServer.addListener("listening", () => this.webSocketServer.addListener("listening", () =>
{ {
this.logger.success( this.logger.success(
this.localisationService.getText("websocket-started", this.httpServerHelper.getWebsocketUrl()), this.localisationService.getText("websocket-started", this.httpServerHelper.getWebsocketUrl()),
@ -49,7 +60,7 @@ export class WebSocketServer
); );
}); });
webSocketServer.addListener("connection", this.wsOnConnection.bind(this)); this.webSocketServer.addListener("connection", this.wsOnConnection.bind(this));
} }
public sendMessage(sessionID: string, output: INotification): void public sendMessage(sessionID: string, output: INotification): void
@ -101,7 +112,7 @@ export class WebSocketServer
const logger = this.logger; const logger = this.logger;
const msgToLog = this.localisationService.getText("websocket-received_message", playerInfoText); const msgToLog = this.localisationService.getText("websocket-received_message", playerInfoText);
ws.on("message", function message(msg) ws.on("message", (msg) =>
{ {
logger.info(`${msgToLog} ${msg}`); logger.info(`${msgToLog} ${msg}`);
}); });