From d792d35d095de09067aeabc697c9e2cb9fc922bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 20 Dec 2021 15:33:47 +0100 Subject: [PATCH] Correctly cleaning room in pusher even if an exception occurs Closes #1646 (again) --- pusher/src/Controller/IoSocketController.ts | 9 +++++---- pusher/src/Services/SocketManager.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index c308ca00..072fe5dd 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -273,7 +273,7 @@ export class IoSocketController { rejected: true, message: err?.response?.data.message, status: err?.response?.status, - room, + roomId, }, websocketKey, websocketProtocol, @@ -368,6 +368,7 @@ export class IoSocketController { rejected: true, reason: e instanceof InvalidTokenError ? tokenInvalidException : null, message: e.message, + roomId, }, websocketKey, websocketProtocol, @@ -380,6 +381,7 @@ export class IoSocketController { rejected: true, reason: null, message: "500 Internal Server Error", + roomId, }, websocketKey, websocketProtocol, @@ -394,9 +396,8 @@ export class IoSocketController { open: (ws) => { if (ws.rejected === true) { // If there is a room in the error, let's check if we need to clean it. - if (ws.room) { - const room = ws.room as PusherRoom; - socketManager.deleteRoomIfEmpty(room); + if (ws.roomId) { + socketManager.deleteRoomIfEmptyFromId(ws.roomId as string); } //FIX ME to use status code diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index 8bff27cb..703b1cda 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -370,7 +370,7 @@ export class SocketManager implements ZoneEventListener { } } - public deleteRoomIfEmpty(room: PusherRoom): void { + private deleteRoomIfEmpty(room: PusherRoom): void { if (room.isEmpty()) { room.close(); this.rooms.delete(room.roomUrl); @@ -378,6 +378,13 @@ export class SocketManager implements ZoneEventListener { } } + public deleteRoomIfEmptyFromId(roomUrl: string): void { + const room = this.rooms.get(roomUrl); + if (room) { + this.deleteRoomIfEmpty(room); + } + } + async getOrCreateRoom(roomUrl: string): Promise { //check and create new world for a room let room = this.rooms.get(roomUrl);