Merge pull request #1659 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

View File

@ -1,5 +1,5 @@
import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." 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 { PointInterface } from "../Model/Websocket/PointInterface";
import { import {
SetPlayerDetailsMessage, SetPlayerDetailsMessage,
@ -273,6 +273,7 @@ export class IoSocketController {
rejected: true, rejected: true,
message: err?.response?.data.message, message: err?.response?.data.message,
status: err?.response?.status, status: err?.response?.status,
room,
}, },
websocketKey, websocketKey,
websocketProtocol, websocketProtocol,
@ -392,6 +393,12 @@ export class IoSocketController {
/* Handlers */ /* Handlers */
open: (ws) => { open: (ws) => {
if (ws.rejected === true) { 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 //FIX ME to use status code
if (ws.reason === tokenInvalidException) { if (ws.reason === tokenInvalidException) {
socketManager.emitTokenExpiredMessage(ws); socketManager.emitTokenExpiredMessage(ws);

View File

@ -351,11 +351,7 @@ export class SocketManager implements ZoneEventListener {
debug("Leaving room %s.", socket.roomId); debug("Leaving room %s.", socket.roomId);
room.leave(socket); room.leave(socket);
if (room.isEmpty()) { this.deleteRoomIfEmpty(room);
room.close();
this.rooms.delete(socket.roomId);
debug("Room %s is empty. Deleting.", socket.roomId);
}
} else { } else {
console.error("Could not find the GameRoom the user is leaving!"); 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> { async getOrCreateRoom(roomUrl: string): Promise<PusherRoom> {
//check and create new world for a room //check and create new world for a room
let room = this.rooms.get(roomUrl); let room = this.rooms.get(roomUrl);