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[],
position: PositionInterface,
viewport: ViewportInterface,
companion: string | null
companion: string | null,
pusherUrl: string | null
): Promise<OnConnectInterface> {
return new Promise<OnConnectInterface>((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));
});
});

View file

@ -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);

View file

@ -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",

View file

@ -187,6 +187,7 @@ export class GameScene extends DirtyScene {
};
private gameMap!: GameMap;
private pusherUrl: string | null = null;
private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>();
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) {

View file

@ -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: ");