diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 05d0255d..76816139 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -274,7 +274,8 @@ class ConnectionManager { characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, - companion: string | null + companion: string | null, + pusherUrl: string | null ): Promise { return new Promise((resolve, reject) => { const connection = new RoomConnection( @@ -284,7 +285,8 @@ class ConnectionManager { characterLayers, position, viewport, - companion + companion, + pusherUrl ); connection.onConnectError((error: object) => { @@ -313,9 +315,15 @@ class ConnectionManager { this.reconnectingTimeout = setTimeout(() => { //todo: allow a way to break recursion? //todo: find a way to avoid recursive function. Otherwise, the call stack will grow indefinitely. - void this.connectToRoomSocket(roomUrl, name, characterLayers, position, viewport, companion).then( - (connection) => resolve(connection) - ); + void this.connectToRoomSocket( + roomUrl, + name, + characterLayers, + position, + viewport, + companion, + pusherUrl + ).then((connection) => resolve(connection)); }, 4000 + Math.floor(Math.random() * 2000)); }); }); diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index 4e2f8397..e2ec27bd 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -155,6 +155,7 @@ export class RoomConnection implements RoomConnection { * @param position * @param viewport * @param companion + * @param pusherUrl */ public constructor( token: string | null, @@ -163,16 +164,17 @@ export class RoomConnection implements RoomConnection { characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, - companion: string | null + companion: string | null, + pusherUrl: string | null ) { - let url = new URL(PUSHER_URL, window.location.toString()).toString(); + let url = new URL(pusherUrl ? pusherUrl : PUSHER_URL, window.location.toString()).toString(); url = url.replace("http://", "ws://").replace("https://", "wss://"); if (!url.endsWith("/")) { url += "/"; } url += "room"; url += "?roomId=" + encodeURIComponent(roomUrl); - url += "&token=" + (token ? encodeURIComponent(token) : ""); + // url += "&token=" + (token ? encodeURIComponent(token) : ""); url += "&name=" + encodeURIComponent(name); for (const layer of characterLayers) { url += "&characterLayers=" + encodeURIComponent(layer); diff --git a/front/src/Phaser/Game/GameMapProperties.ts b/front/src/Phaser/Game/GameMapProperties.ts index 56627ec3..62ceeb01 100644 --- a/front/src/Phaser/Game/GameMapProperties.ts +++ b/front/src/Phaser/Game/GameMapProperties.ts @@ -25,6 +25,7 @@ export enum GameMapProperties { OPEN_WEBSITE_TRIGGER_MESSAGE = "openWebsiteTriggerMessage", PLAY_AUDIO = "playAudio", PLAY_AUDIO_LOOP = "playAudioLoop", + PUSHER_URL = "apiUrl", READABLE_BY = "readableBy", SCRIPT = "script", SCRIPT_DISABLE_MODULE_SUPPORT = "scriptDisableModuleSupport", diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 242d97c8..692d8106 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -187,6 +187,7 @@ export class GameScene extends DirtyScene { }; private gameMap!: GameMap; + private pusherUrl: string | null = null; private actionableItems: Map = new Map(); public userInputManager!: UserInputManager; private isReconnecting: boolean | undefined = undefined; @@ -687,6 +688,8 @@ export class GameScene extends DirtyScene { * Initializes the connection to Pusher. */ private connect(): void { + this.pusherUrl = this.getPusherUrl(this.mapFile); + const camera = this.cameraManager.getCamera(); connectionManager @@ -703,7 +706,8 @@ export class GameScene extends DirtyScene { right: camera.scrollX + camera.width, bottom: camera.scrollY + camera.height, }, - this.companion + this.companion, + this.pusherUrl ) .then((onConnect: OnConnectInterface) => { this.connection = onConnect.connection; @@ -1642,6 +1646,10 @@ ${escapedMessage} ); } + private getPusherUrl(map: ITiledMap): string { + return (this.getProperties(map, GameMapProperties.PUSHER_URL) as string[])[0]; + } + private getProperty(layer: ITiledMapLayer | ITiledMap, name: string): string | boolean | number | undefined { const properties: ITiledMapProperty[] | undefined = layer.properties; if (!properties) { diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 6db53403..bbf7e173 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -195,7 +195,11 @@ export class IoSocketController { const websocketExtensions = req.getHeader("sec-websocket-extensions"); const IPAddress = req.getHeader("x-forwarded-for"); - const roomId = query.roomId; + const roomId = + typeof query.roomId != "string" + ? query.roomId + : "https://dummy.fediventure.net/_/global" + + query.roomId.replace(new RegExp("[^_]*_/[^/]*/"), "/"); try { if (typeof roomId !== "string") { throw new Error("Undefined room ID: ");