diff --git a/back/src/RoomManager.ts b/back/src/RoomManager.ts index 8dbde018..322c9b46 100644 --- a/back/src/RoomManager.ts +++ b/back/src/RoomManager.ts @@ -251,7 +251,12 @@ const roomManager: IRoomManagerServer = { }, sendAdminMessage(call: ServerUnaryCall, callback: sendUnaryData): void { socketManager - .sendAdminMessage(call.request.getRoomid(), call.request.getRecipientuuid(), call.request.getMessage()) + .sendAdminMessage( + call.request.getRoomid(), + call.request.getRecipientuuid(), + call.request.getMessage(), + call.request.getType() + ) .catch((e) => console.error(e)); callback(null, new EmptyMessage()); diff --git a/back/src/Services/SocketManager.ts b/back/src/Services/SocketManager.ts index 8989df75..a5f53f4c 100644 --- a/back/src/Services/SocketManager.ts +++ b/back/src/Services/SocketManager.ts @@ -691,7 +691,7 @@ export class SocketManager { } } - public async sendAdminMessage(roomId: string, recipientUuid: string, message: string): Promise { + public async sendAdminMessage(roomId: string, recipientUuid: string, message: string, type: string): Promise { const room = await this.roomsPromises.get(roomId); if (!room) { console.error( @@ -715,7 +715,7 @@ export class SocketManager { for (const recipient of recipients) { const sendUserMessage = new SendUserMessage(); sendUserMessage.setMessage(message); - sendUserMessage.setType("ban"); //todo: is the type correct? + sendUserMessage.setType(type); const serverToClientMessage = new ServerToClientMessage(); serverToClientMessage.setSendusermessage(sendUserMessage); diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 1f683b3b..072fe5dd 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, + roomId, }, websocketKey, websocketProtocol, @@ -367,6 +368,7 @@ export class IoSocketController { rejected: true, reason: e instanceof InvalidTokenError ? tokenInvalidException : null, message: e.message, + roomId, }, websocketKey, websocketProtocol, @@ -379,6 +381,7 @@ export class IoSocketController { rejected: true, reason: null, message: "500 Internal Server Error", + roomId, }, websocketKey, websocketProtocol, @@ -392,6 +395,11 @@ 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.roomId) { + socketManager.deleteRoomIfEmptyFromId(ws.roomId as string); + } + //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 628ee998..bbcbfb4a 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -359,11 +359,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!"); } @@ -382,6 +378,21 @@ export class SocketManager implements ZoneEventListener { } } + private deleteRoomIfEmpty(room: PusherRoom): void { + if (room.isEmpty()) { + room.close(); + this.rooms.delete(room.roomUrl); + debug("Room %s is empty. Deleting.", room.roomUrl); + } + } + + 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);