Merge pull request from thecodingmachine/fix/room_cleanup_on_error

Properly delete rooms in Pusher in case of connection error and empty room
This commit is contained in:
David Négrier 2021-12-20 14:29:28 +01:00 committed by GitHub
commit 7938a7d610
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions
pusher/src

View File

@ -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);

View File

@ -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<PusherRoom> {
//check and create new world for a room
let room = this.rooms.get(roomUrl);