add onConnection and onDisconnection

This commit is contained in:
nvms 2025-04-18 15:40:55 -04:00
parent b4751aefe8
commit 51fc280d8b
4 changed files with 84 additions and 3 deletions

View File

@ -1089,4 +1089,30 @@ export class MeshServer extends WebSocketServer {
callback();
}
}
/**
* Registers a callback function to be executed when a new connection is established.
*
* @param {(connection: Connection) => Promise<void> | void} callback - The function to execute when a new connection is established.
* @returns {MeshServer} The server instance for method chaining.
*/
onConnection(
callback: (connection: Connection) => Promise<void> | void
): MeshServer {
this.on("connected", callback);
return this;
}
/**
* Registers a callback function to be executed when a connection is closed.
*
* @param {(connection: Connection) => Promise<void> | void} callback - The function to execute when a connection is closed.
* @returns {MeshServer} The server instance for method chaining.
*/
onDisconnection(
callback: (connection: Connection) => Promise<void> | void
): MeshServer {
this.on("disconnected", callback);
return this;
}
}

View File

@ -23,7 +23,9 @@ const flushRedis = async () => {
await redis.quit();
};
describe("KeepAliveServer", () => {
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
describe("MeshServer", () => {
const port = 8126;
let server: MeshServer;
let clientA: MeshClient;
@ -129,5 +131,58 @@ describe("KeepAliveServer", () => {
await server.connectionManager.getAllMetadataForRoom("room-b");
expect(roomBMetadata).toEqual([{ [connectionB.id]: metadataB }]);
});
test("onConnection callback is executed when a client connects", async () => {
let connectionReceived: any = null;
const connectionPromise = new Promise<void>((resolve) => {
server.onConnection((connection) => {
connectionReceived = connection;
resolve();
});
});
await clientA.connect();
await connectionPromise;
expect(connectionReceived).not.toBeNull();
if (!connectionReceived) {
return;
}
expect(connectionReceived.id).toBeDefined();
expect(connectionReceived.isDead).toBe(false);
const connections = server.connectionManager.getLocalConnections();
expect(connections).toContain(connectionReceived);
});
test("onDisconnection callback is executed when a client disconnects", async () => {
let disconnectedConnection: any = null;
const disconnectionPromise = new Promise<void>((resolve) => {
server.onDisconnection((connection) => {
disconnectedConnection = connection;
resolve();
});
});
await clientA.connect();
await wait(100);
const connections = server.connectionManager.getLocalConnections();
const connectionBeforeDisconnect = connections[0];
expect(connectionBeforeDisconnect).toBeDefined();
const connectionId = connectionBeforeDisconnect?.id;
await clientA.close();
await disconnectionPromise;
expect(disconnectedConnection).not.toBeNull();
if (disconnectedConnection && connectionId) {
expect(disconnectedConnection.id).toBe(connectionId);
expect(disconnectedConnection.isDead).toBe(true);
}
});
});
});

View File

@ -23,7 +23,7 @@ const flushRedis = async () => {
await redis.quit();
};
describe("KeepAliveClient", () => {
describe("MeshClient", () => {
const port = 8127;
let server: MeshServer;
let client: MeshClient;

View File

@ -23,7 +23,7 @@ const flushRedis = async () => {
await redis.quit();
};
describe("KeepAliveServer", () => {
describe("MeshServer", () => {
const port = 8128;
let server: MeshServer;
let clientA: MeshClient;