Pusher federation

Source: https://gist.github.com/jstsmthrgk/2c82124c9ac2bd453658f52c90b8dd44
This commit is contained in:
PizZaKatZe 2022-01-04 14:43:45 +01:00 committed by Ludwig Behm
parent f1c999ffc1
commit 185d3b58ed
5 changed files with 33 additions and 10 deletions

View file

@ -274,7 +274,8 @@ class ConnectionManager {
characterLayers: string[], characterLayers: string[],
position: PositionInterface, position: PositionInterface,
viewport: ViewportInterface, viewport: ViewportInterface,
companion: string | null companion: string | null,
pusherUrl: string | null
): Promise<OnConnectInterface> { ): Promise<OnConnectInterface> {
return new Promise<OnConnectInterface>((resolve, reject) => { return new Promise<OnConnectInterface>((resolve, reject) => {
const connection = new RoomConnection( const connection = new RoomConnection(
@ -284,7 +285,8 @@ class ConnectionManager {
characterLayers, characterLayers,
position, position,
viewport, viewport,
companion companion,
pusherUrl
); );
connection.onConnectError((error: object) => { connection.onConnectError((error: object) => {
@ -313,9 +315,15 @@ 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.
void this.connectToRoomSocket(roomUrl, name, characterLayers, position, viewport, companion).then( void this.connectToRoomSocket(
(connection) => resolve(connection) roomUrl,
); name,
characterLayers,
position,
viewport,
companion,
pusherUrl
).then((connection) => resolve(connection));
}, 4000 + Math.floor(Math.random() * 2000)); }, 4000 + Math.floor(Math.random() * 2000));
}); });
}); });

View file

@ -155,6 +155,7 @@ export class RoomConnection implements RoomConnection {
* @param position * @param position
* @param viewport * @param viewport
* @param companion * @param companion
* @param pusherUrl
*/ */
public constructor( public constructor(
token: string | null, token: string | null,
@ -163,16 +164,17 @@ export class RoomConnection implements RoomConnection {
characterLayers: string[], characterLayers: string[],
position: PositionInterface, position: PositionInterface,
viewport: ViewportInterface, 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://"); url = url.replace("http://", "ws://").replace("https://", "wss://");
if (!url.endsWith("/")) { if (!url.endsWith("/")) {
url += "/"; url += "/";
} }
url += "room"; url += "room";
url += "?roomId=" + encodeURIComponent(roomUrl); url += "?roomId=" + encodeURIComponent(roomUrl);
url += "&token=" + (token ? encodeURIComponent(token) : ""); // url += "&token=" + (token ? encodeURIComponent(token) : "");
url += "&name=" + encodeURIComponent(name); url += "&name=" + encodeURIComponent(name);
for (const layer of characterLayers) { for (const layer of characterLayers) {
url += "&characterLayers=" + encodeURIComponent(layer); url += "&characterLayers=" + encodeURIComponent(layer);

View file

@ -25,6 +25,7 @@ export enum GameMapProperties {
OPEN_WEBSITE_TRIGGER_MESSAGE = "openWebsiteTriggerMessage", OPEN_WEBSITE_TRIGGER_MESSAGE = "openWebsiteTriggerMessage",
PLAY_AUDIO = "playAudio", PLAY_AUDIO = "playAudio",
PLAY_AUDIO_LOOP = "playAudioLoop", PLAY_AUDIO_LOOP = "playAudioLoop",
PUSHER_URL = "apiUrl",
READABLE_BY = "readableBy", READABLE_BY = "readableBy",
SCRIPT = "script", SCRIPT = "script",
SCRIPT_DISABLE_MODULE_SUPPORT = "scriptDisableModuleSupport", SCRIPT_DISABLE_MODULE_SUPPORT = "scriptDisableModuleSupport",

View file

@ -187,6 +187,7 @@ export class GameScene extends DirtyScene {
}; };
private gameMap!: GameMap; private gameMap!: GameMap;
private pusherUrl: string | null = null;
private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>(); private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>();
public userInputManager!: UserInputManager; public userInputManager!: UserInputManager;
private isReconnecting: boolean | undefined = undefined; private isReconnecting: boolean | undefined = undefined;
@ -687,6 +688,8 @@ export class GameScene extends DirtyScene {
* Initializes the connection to Pusher. * Initializes the connection to Pusher.
*/ */
private connect(): void { private connect(): void {
this.pusherUrl = this.getPusherUrl(this.mapFile);
const camera = this.cameraManager.getCamera(); const camera = this.cameraManager.getCamera();
connectionManager connectionManager
@ -703,7 +706,8 @@ export class GameScene extends DirtyScene {
right: camera.scrollX + camera.width, right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height, bottom: camera.scrollY + camera.height,
}, },
this.companion this.companion,
this.pusherUrl
) )
.then((onConnect: OnConnectInterface) => { .then((onConnect: OnConnectInterface) => {
this.connection = onConnect.connection; 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 { private getProperty(layer: ITiledMapLayer | ITiledMap, name: string): string | boolean | number | undefined {
const properties: ITiledMapProperty[] | undefined = layer.properties; const properties: ITiledMapProperty[] | undefined = layer.properties;
if (!properties) { if (!properties) {

View file

@ -195,7 +195,11 @@ export class IoSocketController {
const websocketExtensions = req.getHeader("sec-websocket-extensions"); const websocketExtensions = req.getHeader("sec-websocket-extensions");
const IPAddress = req.getHeader("x-forwarded-for"); 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 { try {
if (typeof roomId !== "string") { if (typeof roomId !== "string") {
throw new Error("Undefined room ID: "); throw new Error("Undefined room ID: ");