From f9d765f68bf7883f81780da54be64f72471a25dd Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Tue, 2 Jul 2024 13:35:12 +0100 Subject: [PATCH] Fix duplicate chat connection id crash --- .../src/Networked3dWebExperienceClient.ts | 2 +- .../src/chat-network/ChatNetworkingServer.ts | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/3d-web-experience-client/src/Networked3dWebExperienceClient.ts b/packages/3d-web-experience-client/src/Networked3dWebExperienceClient.ts index 097fa980..477ce3e3 100644 --- a/packages/3d-web-experience-client/src/Networked3dWebExperienceClient.ts +++ b/packages/3d-web-experience-client/src/Networked3dWebExperienceClient.ts @@ -360,7 +360,7 @@ export class Networked3dWebExperienceClient { this.networkChat = new ChatNetworkingClient({ url: this.config.chatNetworkAddress, sessionToken: this.config.sessionToken, - websocketFactory: (url: string) => new WebSocket(`${url}?id=${this.clientId}`), + websocketFactory: (url: string) => new WebSocket(url), statusUpdateCallback: (status: WebsocketStatus) => { if (status === WebsocketStatus.Disconnected || status === WebsocketStatus.Reconnecting) { // The connection was lost after being established - the connection may be re-established with a different client ID diff --git a/packages/3d-web-text-chat/src/chat-network/ChatNetworkingServer.ts b/packages/3d-web-text-chat/src/chat-network/ChatNetworkingServer.ts index 04c4b671..ca0a6471 100644 --- a/packages/3d-web-text-chat/src/chat-network/ChatNetworkingServer.ts +++ b/packages/3d-web-text-chat/src/chat-network/ChatNetworkingServer.ts @@ -108,7 +108,13 @@ export class ChatNetworkingServer { return; } if (this.clientsById.has(authResponse.id)) { - throw new Error(`Client already connected with ID: ${authResponse.id}`); + /* + This client is already connected. Reject the connection. If the old connection is abandoned then it will + be removed and retry attempts will succeed. + */ + console.error(`Client already connected with ID: ${authResponse.id}`); + socket.close(); + return; } client.id = authResponse.id; this.clientsById.set(client.id, client);