Heavy changes: refactoring the pusher to always send the textures (and the front to accept them)

This commit is contained in:
David Négrier
2022-02-23 21:08:21 +01:00
parent d65fe0ee26
commit 378a95962a
31 changed files with 290 additions and 270 deletions
@@ -273,7 +273,6 @@ export class AuthenticateController extends BaseHttpController {
const email = data.email;
const roomUrl = data.roomUrl;
const mapUrlStart = data.mapUrlStart;
const textures = data.textures;
const authToken = jwtTokenManager.createAuthToken(email || userUuid);
res.json({
@@ -283,7 +282,6 @@ export class AuthenticateController extends BaseHttpController {
roomUrl,
mapUrlStart,
organizationMemberToken,
textures,
} as RegisterData);
} catch (e) {
console.error("register => ERROR", e);
+75 -14
View File
@@ -1,4 +1,4 @@
import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import { ExSocketInterface } from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import { GameRoomPolicyTypes } from "../Model/PusherRoom";
import { PointInterface } from "../Model/Websocket/PointInterface";
import {
@@ -31,11 +31,46 @@ import { emitInBatch } from "../Services/IoSocketHelpers";
import { ADMIN_API_URL, ADMIN_SOCKETS_TOKEN, DISABLE_ANONYMOUS, SOCKET_IDLE_TIMER } from "../Enum/EnvironmentVariable";
import { Zone } from "_Model/Zone";
import { ExAdminSocketInterface } from "_Model/Websocket/ExAdminSocketInterface";
import { CharacterTexture } from "../Messages/JsonMessages/CharacterTexture";
import { isAdminMessageInterface } from "../Model/Websocket/Admin/AdminMessages";
import Axios from "axios";
import { InvalidTokenError } from "../Controller/InvalidTokenError";
import HyperExpress from "hyper-express";
import { localWokaService } from "../Services/LocalWokaService";
import { WebSocket } from "uWebSockets.js";
import { WokaDetail } from "../Enum/PlayerTextures";
/**
* The object passed between the "open" and the "upgrade" methods when opening a websocket
*/
interface UpgradeData {
// Data passed here is accessible on the "websocket" socket object.
rejected: false;
token: string;
userUuid: string;
IPAddress: string;
roomId: string;
name: string;
companion: CompanionMessage | undefined;
characterLayers: WokaDetail[];
messages: unknown[];
tags: string[];
visitCardUrl: string | null;
userRoomToken: string | undefined;
position: PointInterface;
viewport: {
top: number;
right: number;
bottom: number;
left: number;
};
}
interface UpgradeFailedData {
rejected: true;
reason: "tokenInvalid" | "textureInvalid" | null;
message: string;
roomId: string;
}
export class IoSocketController {
private nextUserId: number = 1;
@@ -244,7 +279,7 @@ export class IoSocketController {
let memberVisitCardUrl: string | null = null;
let memberMessages: unknown;
let memberUserRoomToken: string | undefined;
let memberTextures: CharacterTexture[] = [];
let memberTextures: WokaDetail[] = [];
const room = await socketManager.getOrCreateRoom(roomId);
let userData: FetchMemberDataByUuidResponse = {
email: userIdentifier,
@@ -256,6 +291,9 @@ export class IoSocketController {
anonymous: true,
userRoomToken: undefined,
};
let characterLayerObjs: WokaDetail[];
if (ADMIN_API_URL) {
try {
try {
@@ -308,6 +346,8 @@ export class IoSocketController {
) {
throw new Error("Use the login URL to connect");
}
characterLayerObjs = memberTextures;
} catch (e) {
console.log(
"access not granted for user " +
@@ -318,11 +358,31 @@ export class IoSocketController {
console.error(e);
throw new Error("User cannot access this world");
}
} else {
const fetchedTextures = await localWokaService.fetchWokaDetails(characterLayers);
if (fetchedTextures === undefined) {
// The textures we want to use do not exist!
// We need to go in error.
res.upgrade(
{
rejected: true,
reason: "textureInvalid",
message: "",
roomId,
} as UpgradeFailedData,
websocketKey,
websocketProtocol,
websocketExtensions,
context
);
return;
}
characterLayerObjs = fetchedTextures;
}
// Generate characterLayers objects from characterLayers string[]
const characterLayerObjs: CharacterLayer[] =
SocketManager.mergeCharacterLayersAndCustomTextures(characterLayers, memberTextures);
/*const characterLayerObjs: CharacterLayer[] =
SocketManager.mergeCharacterLayersAndCustomTextures(characterLayers, memberTextures);*/
if (upgradeAborted.aborted) {
console.log("Ouch! Client disconnected before we could upgrade it!");
@@ -334,7 +394,7 @@ export class IoSocketController {
res.upgrade(
{
// Data passed here is accessible on the "websocket" socket object.
url,
rejected: false,
token,
userUuid: userData.userUuid,
IPAddress,
@@ -346,7 +406,6 @@ export class IoSocketController {
tags: memberTags,
visitCardUrl: memberVisitCardUrl,
userRoomToken: memberUserRoomToken,
textures: memberTextures,
position: {
x: x,
y: y,
@@ -359,7 +418,7 @@ export class IoSocketController {
bottom,
left,
},
},
} as UpgradeData,
/* Spell these correctly */
websocketKey,
websocketProtocol,
@@ -374,7 +433,7 @@ export class IoSocketController {
reason: e instanceof InvalidTokenError ? tokenInvalidException : null,
message: e.message,
roomId,
},
} as UpgradeFailedData,
websocketKey,
websocketProtocol,
websocketExtensions,
@@ -387,7 +446,7 @@ export class IoSocketController {
reason: null,
message: "500 Internal Server Error",
roomId,
},
} as UpgradeFailedData,
websocketKey,
websocketProtocol,
websocketExtensions,
@@ -398,20 +457,23 @@ export class IoSocketController {
})();
},
/* Handlers */
open: (ws) => {
open: (_ws: WebSocket) => {
const ws = _ws as WebSocket & (UpgradeData | UpgradeFailedData);
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);
socketManager.deleteRoomIfEmptyFromId(ws.roomId);
}
//FIX ME to use status code
if (ws.reason === tokenInvalidException) {
socketManager.emitTokenExpiredMessage(ws);
} else if (ws.reason === "textureInvalid") {
socketManager.emitInvalidTextureMessage(ws);
} else if (ws.message === "World is full") {
socketManager.emitWorldFullMessage(ws);
} else {
socketManager.emitConnexionErrorMessage(ws, ws.message as string);
socketManager.emitConnexionErrorMessage(ws, ws.message);
}
setTimeout(() => ws.close(), 0);
return;
@@ -535,7 +597,6 @@ export class IoSocketController {
client.name = ws.name;
client.tags = ws.tags;
client.visitCardUrl = ws.visitCardUrl;
client.textures = ws.textures;
client.characterLayers = ws.characterLayers;
client.companion = ws.companion;
client.roomId = ws.roomId;
+5 -16
View File
@@ -1,24 +1,13 @@
import { hasToken } from "../Middleware/HasToken";
import { BaseHttpController } from "./BaseHttpController";
import { ADMIN_API_URL } from "../Enum/EnvironmentVariable";
import { adminWokaService } from "..//Services/AdminWokaService";
import { localWokaService } from "..//Services/LocalWokaService";
import { WokaServiceInterface } from "src/Services/WokaServiceInterface";
import { Server } from "hyper-express";
import { wokaService } from "../Services/WokaService";
export class WokaListController extends BaseHttpController {
private wokaService: WokaServiceInterface;
constructor(app: Server) {
super(app);
this.wokaService = ADMIN_API_URL ? adminWokaService : localWokaService;
}
routes() {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.app.get("/woka-list", {}, async (req, res) => {
const token = req.header("Authorization");
const wokaList = await this.wokaService.getWokaList(token);
const wokaList = await wokaService.getWokaList(token);
if (!wokaList) {
return res.status(500).send("Error on getting woka list");
@@ -28,20 +17,20 @@ export class WokaListController extends BaseHttpController {
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.app.post("/woka-details", async (req, res) => {
/*this.app.post("/woka-details", async (req, res) => {
const body = await req.json();
if (!body || !body.textureIds) {
return res.status(400);
}
const textureIds = body.textureIds;
const wokaDetails = await this.wokaService.fetchWokaDetails(textureIds);
const wokaDetails = await wokaService.fetchWokaDetails(textureIds);
if (!wokaDetails) {
return res.json({ details: [] });
}
return res.json(wokaDetails);
});
});*/
}
}