From a565961881c1276426c27841d603b363e8f7b739 Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Tue, 30 Apr 2024 22:30:31 +0300 Subject: [PATCH 1/2] Fix conflicting user id --- .../src/UserNetworkingServer.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/3d-web-user-networking/src/UserNetworkingServer.ts b/packages/3d-web-user-networking/src/UserNetworkingServer.ts index 45ab6461..baec6fb1 100644 --- a/packages/3d-web-user-networking/src/UserNetworkingServer.ts +++ b/packages/3d-web-user-networking/src/UserNetworkingServer.ts @@ -41,7 +41,7 @@ export type UserNetworkingServerOptions = { }; export class UserNetworkingServer { - private allClients = new Set(); + private allClientsById = new Map(); private authenticatedClientsById: Map = new Map(); constructor(private options: UserNetworkingServerOptions) { @@ -52,7 +52,7 @@ export class UserNetworkingServer { private heartBeat() { const now = Date.now(); - this.allClients.forEach((client) => { + this.allClientsById.forEach((client) => { if (now - client.lastPong > heartBeatRate) { client.socket.close(); this.handleDisconnectedClient(client); @@ -70,7 +70,7 @@ export class UserNetworkingServer { private getId(): number { let id = 1; - while (this.authenticatedClientsById.has(id)) { + while (this.allClientsById.has(id)) { id++; } return id; @@ -93,7 +93,7 @@ export class UserNetworkingServer { state: 0, }, }; - this.allClients.add(client); + this.allClientsById.set(id, client); socket.on("message", (message: WebSocket.Data, _isBinary: boolean) => { if (message instanceof Buffer) { @@ -147,10 +147,10 @@ export class UserNetworkingServer { } private handleDisconnectedClient(client: Client) { - if (!this.allClients.has(client)) { + if (!this.allClientsById.has(client.id)) { return; } - this.allClients.delete(client); + this.allClientsById.delete(client.id); if (client.authenticatedUser !== null) { // Only report disconnections of clients that were authenticated this.options.onClientDisconnect(client.id); From 0d8773c15fe80af1a51e5c0deee1d67e3f44c905 Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Tue, 30 Apr 2024 22:55:30 +0300 Subject: [PATCH 2/2] Fix tests --- .../3d-web-user-networking/test/UserNetworking.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/3d-web-user-networking/test/UserNetworking.test.ts b/packages/3d-web-user-networking/test/UserNetworking.test.ts index 2acbc54f..02d999a3 100644 --- a/packages/3d-web-user-networking/test/UserNetworking.test.ts +++ b/packages/3d-web-user-networking/test/UserNetworking.test.ts @@ -91,7 +91,7 @@ describe("UserNetworking", () => { expect(await user1IdentityPromise).toEqual(1); await waitUntil( - () => (server as any).allClients.size === 1, + () => (server as any).allClientsById.size === 1, "wait for server to see the presence of user 1", ); @@ -138,7 +138,7 @@ describe("UserNetworking", () => { expect(await user2IdentityPromise).toEqual(2); await waitUntil( - () => (server as any).allClients.size === 2, + () => (server as any).allClientsById.size === 2, "wait for server to see the presence of user 2", ); @@ -209,7 +209,7 @@ describe("UserNetworking", () => { user2.stop(); await waitUntil( - () => (server as any).allClients.size === 1, + () => (server as any).allClientsById.size === 1, "wait for server to see the removal of user 2", ); @@ -221,7 +221,7 @@ describe("UserNetworking", () => { user1.stop(); await waitUntil( - () => (server as any).allClients.size === 0, + () => (server as any).allClientsById.size === 0, "wait for server to see the removal of user 1", );