From e0aab9c0ce164eed26104dddad0e713436fd8bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 20 Dec 2021 14:12:58 +0100 Subject: [PATCH] Properly delete rooms in Pusher in case of connection error and empty room Closes #1646 --- pusher/src/Controller/IoSocketController.ts | 9 ++++++++- pusher/src/Services/SocketManager.ts | 14 +++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 1f683b3b..c308ca00 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -1,5 +1,5 @@ import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." -import { GameRoomPolicyTypes } from "../Model/PusherRoom"; +import { GameRoomPolicyTypes, PusherRoom } from "../Model/PusherRoom"; import { PointInterface } from "../Model/Websocket/PointInterface"; import { SetPlayerDetailsMessage, @@ -273,6 +273,7 @@ export class IoSocketController { rejected: true, message: err?.response?.data.message, status: err?.response?.status, + room, }, websocketKey, websocketProtocol, @@ -392,6 +393,12 @@ export class IoSocketController { /* Handlers */ 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); + } + //FIX ME to use status code if (ws.reason === tokenInvalidException) { socketManager.emitTokenExpiredMessage(ws); diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index a1777f0d..8bff27cb 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -351,11 +351,7 @@ export class SocketManager implements ZoneEventListener { debug("Leaving room %s.", socket.roomId); room.leave(socket); - if (room.isEmpty()) { - room.close(); - this.rooms.delete(socket.roomId); - debug("Room %s is empty. Deleting.", socket.roomId); - } + this.deleteRoomIfEmpty(room); } else { console.error("Could not find the GameRoom the user is leaving!"); } @@ -374,6 +370,14 @@ export class SocketManager implements ZoneEventListener { } } + public deleteRoomIfEmpty(room: PusherRoom): void { + if (room.isEmpty()) { + room.close(); + this.rooms.delete(room.roomUrl); + debug("Room %s is empty. Deleting.", room.roomUrl); + } + } + async getOrCreateRoom(roomUrl: string): Promise { //check and create new world for a room let room = this.rooms.get(roomUrl);