Front federation (API and Jitsi)

This commit is contained in:
PizZaKatZe 2021-03-22 23:08:57 +01:00 committed by Thomas Basler
parent 1981e3102f
commit 9d66085623
3 changed files with 31 additions and 6 deletions

View File

@ -88,9 +88,9 @@ class ConnectionManager {
this.localUser = new LocalUser('', 'test', []); this.localUser = new LocalUser('', 'test', []);
} }
public connectToRoomSocket(roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null): Promise<OnConnectInterface> { public connectToRoomSocket(apiUrl: string, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null): Promise<OnConnectInterface> {
return new Promise<OnConnectInterface>((resolve, reject) => { return new Promise<OnConnectInterface>((resolve, reject) => {
const connection = new RoomConnection(this.localUser.jwtToken, roomId, name, characterLayers, position, viewport, companion); const connection = new RoomConnection(apiUrl, this.localUser.jwtToken, roomId, name, characterLayers, position, viewport, companion);
connection.onConnectError((error: object) => { connection.onConnectError((error: object) => {
console.log('An error occurred while connecting to socket server. Retrying'); console.log('An error occurred while connecting to socket server. Retrying');
reject(error); reject(error);
@ -111,7 +111,7 @@ class ConnectionManager {
this.reconnectingTimeout = setTimeout(() => { this.reconnectingTimeout = setTimeout(() => {
//todo: allow a way to break recursion? //todo: allow a way to break recursion?
//todo: find a way to avoid recursive function. Otherwise, the call stack will grow indefinitely. //todo: find a way to avoid recursive function. Otherwise, the call stack will grow indefinitely.
this.connectToRoomSocket(roomId, name, characterLayers, position, viewport, companion).then((connection) => resolve(connection)); this.connectToRoomSocket(apiUrl, roomId, name, characterLayers, position, viewport, companion).then((connection) => resolve(connection));
}, 4000 + Math.floor(Math.random() * 2000) ); }, 4000 + Math.floor(Math.random() * 2000) );
}); });
}); });

View File

@ -70,8 +70,8 @@ export class RoomConnection implements RoomConnection {
* @param token A JWT token containing the UUID of the user * @param token A JWT token containing the UUID of the user
* @param roomId The ID of the room in the form "_/[instance]/[map_url]" or "@/[org]/[event]/[map]" * @param roomId The ID of the room in the form "_/[instance]/[map_url]" or "@/[org]/[event]/[map]"
*/ */
public constructor(token: string|null, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null) { public constructor(apiUrl: string, token: string|null, roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface, companion: string|null) {
let url = new URL(PUSHER_URL, window.location.toString()).toString(); let url = new URL(apiUrl, window.location.toString()).toString();
url = url.replace('http://', 'ws://').replace('https://', 'wss://'); url = url.replace('http://', 'ws://').replace('https://', 'wss://');
if (!url.endsWith('/')) { if (!url.endsWith('/')) {
url += '/'; url += '/';

View File

@ -13,8 +13,10 @@ import {hasMovedEventName, Player, requestEmoteEventName} from "../Player/Player
import { import {
DEBUG_MODE, DEBUG_MODE,
JITSI_PRIVATE_MODE, JITSI_PRIVATE_MODE,
JITSI_URL,
MAX_PER_GROUP, MAX_PER_GROUP,
POSITION_DELAY, POSITION_DELAY,
PUSHER_URL
} from "../../Enum/EnvironmentVariable"; } from "../../Enum/EnvironmentVariable";
import type { import type {
ITiledMap, ITiledMap,
@ -134,6 +136,24 @@ interface DeleteGroupEventInterface {
groupId: number groupId: number
} }
interface MapProperty {
name: string;
type: string;
value: string|number;
}
// FIXME: See onMapLoad
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getMapProperty(mapData: any, name: string, type: string, dflt: string|undefined = undefined): any {
if (!Array.isArray(mapData.properties)) {
return dflt;
}
const properties: MapProperty[] = mapData.properties;
const prop = properties.find((prop: MapProperty) => prop.type === type && prop.name === name)
return prop ? prop.value : dflt;
}
const defaultStartLayerName = 'start'; const defaultStartLayerName = 'start';
export class GameScene extends DirtyScene implements CenterListener { export class GameScene extends DirtyScene implements CenterListener {
@ -167,6 +187,7 @@ export class GameScene extends DirtyScene implements CenterListener {
MapUrlFile: string; MapUrlFile: string;
RoomId: string; RoomId: string;
instance: string; instance: string;
apiUrl: string = PUSHER_URL;
currentTick!: number; currentTick!: number;
lastSentTick!: number; // The last tick at which a position was sent. lastSentTick!: number; // The last tick at which a position was sent.
@ -292,6 +313,9 @@ export class GameScene extends DirtyScene implements CenterListener {
// FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving. // FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving.
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
private async onMapLoad(data: any): Promise<void> { private async onMapLoad(data: any): Promise<void> {
this.apiUrl = getMapProperty(data.data, 'apiUrl', 'string', PUSHER_URL);
console.info('apiUrl:', this.apiUrl);
// Triggered when the map is loaded // Triggered when the map is loaded
// Load tiles attached to the map recursively // Load tiles attached to the map recursively
this.mapFile = data.data; this.mapFile = data.data;
@ -539,6 +563,7 @@ export class GameScene extends DirtyScene implements CenterListener {
const camera = this.cameras.main; const camera = this.cameras.main;
connectionManager.connectToRoomSocket( connectionManager.connectToRoomSocket(
this.apiUrl,
this.RoomId, this.RoomId,
this.playerName, this.playerName,
this.characterLayers, this.characterLayers,
@ -1583,7 +1608,7 @@ ${escapedMessage}
const allProps = this.gameMap.getCurrentProperties(); const allProps = this.gameMap.getCurrentProperties();
const jitsiConfig = this.safeParseJSONstring(allProps.get("jitsiConfig") as string|undefined, 'jitsiConfig'); const jitsiConfig = this.safeParseJSONstring(allProps.get("jitsiConfig") as string|undefined, 'jitsiConfig');
const jitsiInterfaceConfig = this.safeParseJSONstring(allProps.get("jitsiInterfaceConfig") as string|undefined, 'jitsiInterfaceConfig'); const jitsiInterfaceConfig = this.safeParseJSONstring(allProps.get("jitsiInterfaceConfig") as string|undefined, 'jitsiInterfaceConfig');
const jitsiUrl = allProps.get("jitsiUrl") as string|undefined; const jitsiUrl = (allProps.get("jitsiUrl") as string|undefined) || getMapProperty(this.mapFile, 'jitsiUrl', 'string', JITSI_URL);
jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl); jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl);
this.connection?.setSilent(true); this.connection?.setSilent(true);