Skip to content

Commit

Permalink
disconnect clients from server proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Feb 22, 2025
1 parent 640f41c commit ec7a732
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/server/wsJsonServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { WebSocketServer } from "ws";
import debug from "debug";
import { Server as HttpServer, IncomingMessage, ServerResponse } from "http";
import { Server as HttpsServer } from "https";
import { IncomingMessage, Server as HttpServer, ServerResponse } from "http";
import { WebSocket, WebSocketServer } from "ws";
import { WsJsonClient } from "../client/wsJsonClient.js";
import debug from "debug";
import WsJsonServerProxy from "./wsJsonServerProxy.js";
import { isEmpty } from "lodash-es";
import { Disposable } from "./disposable.js";

const logger = debug("wsJsonServer");
const DEFAULT_PORT = 8080;
Expand All @@ -23,9 +21,9 @@ type DefaultHttpServer = HttpServer<
* a "request" property that matches a method on the WsJsonClient interface and an "args" property that is an array of
* arguments to pass to the method. The response is then forwarded back to the client as a JSON string.
*/
export class WsJsonServer implements Disposable {
export class WsJsonServer {
private readonly wss: WebSocketServer;
private proxies: WsJsonServerProxy[] = [];
private readonly activeClients: Map<WebSocket, WsJsonServerProxy> = new Map();

constructor(
private readonly wsJsonClientFactory: () => WsJsonClient,
Expand All @@ -36,19 +34,30 @@ export class WsJsonServer implements Disposable {
}

start() {
const { wss, server, port, wsJsonClientFactory, proxies } = this;
const { wss, server, port, wsJsonClientFactory, activeClients } = this;

wss.on("connection", (ws) => {
logger("client connected");
proxies.push(new WsJsonServerProxy(ws, wsJsonClientFactory));
ws.on("error", console.error);
ws.on("close", () => {
logger("client disconnected");
const client = activeClients.get(ws);
if (client) {
client.disconnect();
activeClients.delete(ws);
} else {
logger("[warn] client disconnected but no client found");
}
});
activeClients.set(ws, new WsJsonServerProxy(ws, wsJsonClientFactory));
});

wss.on("close", function close() {
logger("server closed");
activeClients.forEach((proxy) => proxy.disconnect());
});

server.listen(port);
logger(`server started and listening on port ${port}`);
}

disconnect() {
const { proxies } = this;
while (!isEmpty(proxies)) {
proxies.pop()?.disconnect();
}
}
}

0 comments on commit ec7a732

Please sign in to comment.