From 9bdcc9da98225d5569519555ce69fd2d60095f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 29 Jun 2020 19:14:54 +0200 Subject: [PATCH 1/2] Deleting world when empty. --- back/src/Controller/IoSocketController.ts | 3 +++ back/src/Model/World.ts | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index edda6de9..8af857fe 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -297,6 +297,9 @@ export class IoSocketController { const world : World|undefined = this.Worlds.get(Client.roomId); if(world){ world.leave(Client); + if (world.isEmpty()) { + this.Worlds.delete(Client.roomId); + } } //user leave previous room Client.leave(Client.roomId); diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 51129857..2cb92dd2 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -72,6 +72,10 @@ export class World { this.users.delete(user.userId); } + public isEmpty(): boolean { + return this.users.size === 0; + } + public updatePosition(socket : Identificable, userPosition: PointInterface): void { const user = this.users.get(socket.userId); if(typeof user === 'undefined') { From 981fa84aa7ae2d8a4a6ac146ca2c6efe6e3cb5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Mon, 29 Jun 2020 19:16:15 +0200 Subject: [PATCH 2/2] Mitigating problem when there is a synchronization issue between World and sockets list. --- back/src/Controller/IoSocketController.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 8af857fe..2ea25f31 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -183,9 +183,13 @@ export class IoSocketController { // The answer shall contain the list of all users of the room with their positions: const listOfUsers = Array.from(world.getUsers(), ([key, user]) => { - const player = this.searchClientByIdOrFail(user.id); + const player: ExSocketInterface|undefined = this.sockets.get(user.id); + if (player === undefined) { + console.warn('Something went wrong. The World contains a user "'+user.id+"' but this user does not exist in the sockets list!"); + return null; + } return new MessageUserPosition(user.id, player.name, player.character, player.position); - }); + }).filter((item: MessageUserPosition|null) => item !== null); answerFn(listOfUsers); } catch (e) { console.error('An error occurred on "join_room" event');