From f4faaa43f1c9049351b012f7a07e19c56ae66e92 Mon Sep 17 00:00:00 2001 From: Kevin Kipp Date: Thu, 15 Aug 2024 16:30:29 -0500 Subject: [PATCH] Try to retrieve user in onConnect In case they're reconnecting! --- app/durableObjects/ChatRoom.server.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/durableObjects/ChatRoom.server.ts b/app/durableObjects/ChatRoom.server.ts index 36777773..3b408b05 100644 --- a/app/durableObjects/ChatRoom.server.ts +++ b/app/durableObjects/ChatRoom.server.ts @@ -58,17 +58,20 @@ export class ChatRoom extends Server { const username = await getUsername(ctx.request) assertNonNullable(username) - const user: User = { - id: connection.id, - name: username, - joined: false, - raisedHand: false, - speaking: false, - tracks: { - audioEnabled: false, - videoEnabled: false, - screenShareEnabled: false, - }, + let user = await this.ctx.storage.get(`session-${connection.id}`) + if (user === undefined) { + user = { + id: connection.id, + name: username, + joined: false, + raisedHand: false, + speaking: false, + tracks: { + audioEnabled: false, + videoEnabled: false, + screenShareEnabled: false, + }, + } } // store the user's data in storage