From 8abc34c63107e5da05a1e831450317c2ab3b5550 Mon Sep 17 00:00:00 2001 From: kharhamel Date: Fri, 5 Mar 2021 18:25:27 +0100 Subject: [PATCH] FEATURE: the connexion error an user can get when a world is full is now correctly traited. Also remove a redundant adminApi call --- front/src/Administration/TypeMessage.ts | 6 +- front/src/Connexion/ConnectionManager.ts | 11 ++ front/src/Connexion/ConnexionModels.ts | 23 ---- front/src/Connexion/RoomConnection.ts | 6 +- front/src/Phaser/Game/GameScene.ts | 124 +++++++----------- front/src/WebRtc/AudioManager.ts | 23 +++- pusher/src/Controller/IoSocketController.ts | 64 +++++---- .../src/Model/Websocket/ExSocketInterface.ts | 1 + pusher/src/Services/AdminApi.ts | 6 +- pusher/src/Services/IoSocketHelpers.ts | 3 +- pusher/src/Services/SocketManager.ts | 75 +---------- 11 files changed, 127 insertions(+), 215 deletions(-) diff --git a/front/src/Administration/TypeMessage.ts b/front/src/Administration/TypeMessage.ts index 61891604..182f7e01 100644 --- a/front/src/Administration/TypeMessage.ts +++ b/front/src/Administration/TypeMessage.ts @@ -77,8 +77,10 @@ export class TypeMessageExt implements TypeMessageInterface{ } } } -export class Ban extends TypeMessageExt { -} + +export class Message extends TypeMessageExt {} + +export class Ban extends TypeMessageExt {} export class Banned extends TypeMessageExt { showMessage(message: string){ diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index de8cd234..0b6e9ff1 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -6,11 +6,22 @@ import {GameConnexionTypes, urlManager} from "../Url/UrlManager"; import {localUserStore} from "./LocalUserStore"; import {LocalUser} from "./LocalUser"; import {Room} from "./Room"; +import {Subject} from "rxjs"; + +export enum ConnexionMessageEventTypes { + worldFull = 1, +} + +export interface ConnexionMessageEvent { + type: ConnexionMessageEventTypes, +} class ConnectionManager { private localUser!:LocalUser; private connexionType?: GameConnexionTypes + + public _connexionMessageStream:Subject = new Subject(); /** * Tries to login to the node server and return the starting map url to be loaded */ diff --git a/front/src/Connexion/ConnexionModels.ts b/front/src/Connexion/ConnexionModels.ts index a0ae3119..d7c49f63 100644 --- a/front/src/Connexion/ConnexionModels.ts +++ b/front/src/Connexion/ConnexionModels.ts @@ -42,14 +42,6 @@ export interface PointInterface { moving: boolean; } -export class Point implements PointInterface{ - constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) { - if(x === null || y === null){ - throw Error("position x and y cannot be null"); - } - } -} - export interface MessageUserPositionInterface { userId: number; name: string; @@ -80,20 +72,10 @@ export interface GroupCreatedUpdatedMessageInterface { groupSize: number } -export interface WebRtcStartMessageInterface { - roomId: string, - clients: UserSimplePeerInterface[] -} - export interface WebRtcDisconnectMessageInterface { userId: number } -export interface WebRtcSignalSentMessageInterface { - receiverId: number, - signal: SignalData -} - export interface WebRtcSignalReceivedMessageInterface { userId: number, signal: SignalData, @@ -113,11 +95,6 @@ export interface ViewportInterface { bottom: number, } -export interface BatchedMessageInterface { - event: string, - payload: unknown -} - export interface ItemEventMessageInterface { itemId: number, event: string, diff --git a/front/src/Connexion/RoomConnection.ts b/front/src/Connexion/RoomConnection.ts index 7d48946b..afc7abde 100644 --- a/front/src/Connexion/RoomConnection.ts +++ b/front/src/Connexion/RoomConnection.ts @@ -43,6 +43,7 @@ import { } from "./ConnexionModels"; import {BodyResourceDescriptionInterface} from "../Phaser/Entity/PlayerTextures"; import {adminMessagesService} from "./AdminMessagesService"; +import {connectionManager, ConnexionMessageEventTypes} from "./ConnectionManager"; const manualPingDelay = 20000; @@ -101,7 +102,7 @@ export class RoomConnection implements RoomConnection { } // If we are not connected yet (if a JoinRoomMessage was not sent), we need to retry. - if (this.userId === null) { + if (this.userId === null && !this.closed) { this.dispatch(EventMessage.CONNECTING_ERROR, event); } }); @@ -156,7 +157,8 @@ export class RoomConnection implements RoomConnection { } as RoomJoinedMessageInterface }); } else if (message.hasErrormessage()) { - console.error(EventMessage.MESSAGE_ERROR, message.getErrormessage()?.getMessage()); + connectionManager._connexionMessageStream.next({type: ConnexionMessageEventTypes.worldFull}); //todo: generalize this behavior to all messages + this.closed = true; } else if (message.hasWebrtcsignaltoclientmessage()) { this.dispatch(EventMessage.WEBRTC_SIGNAL, message.getWebrtcsignaltoclientmessage()); } else if (message.hasWebrtcscreensharingsignaltoclientmessage()) { diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index cc3378a6..d73adcc6 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -3,25 +3,15 @@ import { GroupCreatedUpdatedMessageInterface, MessageUserJoined, MessageUserMovedInterface, - MessageUserPositionInterface, OnConnectInterface, + MessageUserPositionInterface, + OnConnectInterface, PointInterface, PositionInterface, RoomJoinedMessageInterface } from "../../Connexion/ConnexionModels"; import {CurrentGamerInterface, hasMovedEventName, Player} from "../Player/Player"; -import { - DEBUG_MODE, - JITSI_PRIVATE_MODE, - POSITION_DELAY, - RESOLUTION, - ZOOM_LEVEL -} from "../../Enum/EnvironmentVariable"; -import { - ITiledMap, - ITiledMapLayer, - ITiledMapLayerProperty, ITiledMapObject, - ITiledTileSet -} from "../Map/ITiledMap"; +import {DEBUG_MODE, JITSI_PRIVATE_MODE, POSITION_DELAY, RESOLUTION, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable"; +import {ITiledMap, ITiledMapLayer, ITiledMapLayerProperty, ITiledMapObject, ITiledTileSet} from "../Map/ITiledMap"; import {AddPlayerInterface} from "./AddPlayerInterface"; import {PlayerAnimationNames} from "../Player/Animation"; import {PlayerMovement} from "./PlayerMovement"; @@ -41,11 +31,6 @@ import { TRIGGER_WEBSITE_PROPERTIES, WEBSITE_MESSAGE_PROPERTIES } from "../../WebRtc/LayoutManager"; -import Texture = Phaser.Textures.Texture; -import Sprite = Phaser.GameObjects.Sprite; -import CanvasTexture = Phaser.Textures.CanvasTexture; -import GameObject = Phaser.GameObjects.GameObject; -import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import {GameMap} from "./GameMap"; import {coWebsiteManager} from "../../WebRtc/CoWebsiteManager"; import {mediaManager} from "../../WebRtc/MediaManager"; @@ -54,7 +39,7 @@ import {ActionableItem} from "../Items/ActionableItem"; import {UserInputManager} from "../UserInput/UserInputManager"; import {UserMovedMessage} from "../../Messages/generated/messages_pb"; import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils"; -import {connectionManager} from "../../Connexion/ConnectionManager"; +import {connectionManager, ConnexionMessageEvent, ConnexionMessageEventTypes} from "../../Connexion/ConnectionManager"; import {RoomConnection} from "../../Connexion/RoomConnection"; import {GlobalMessageManager} from "../../Administration/GlobalMessageManager"; import {userMessageManager} from "../../Administration/UserMessageManager"; @@ -72,6 +57,12 @@ import {TextureError} from "../../Exception/TextureError"; import {addLoader} from "../Components/Loader"; import {ErrorSceneName} from "../Reconnecting/ErrorScene"; import {localUserStore} from "../../Connexion/LocalUserStore"; +import Texture = Phaser.Textures.Texture; +import Sprite = Phaser.GameObjects.Sprite; +import CanvasTexture = Phaser.Textures.CanvasTexture; +import GameObject = Phaser.GameObjects.GameObject; +import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; +import {Subscription} from "rxjs"; export interface GameSceneInitInterface { initPosition: PointInterface|null, @@ -157,11 +148,12 @@ export class GameScene extends ResizableScene implements CenterListener { // The item that can be selected by pressing the space key. private outlinedItem: ActionableItem|null = null; public userInputManager!: UserInputManager; - private isReconnecting: boolean = false; + private isReconnecting: boolean|undefined = undefined; private startLayerName!: string | null; private openChatIcon!: OpenChatIcon; private playerName!: string; private characterLayers!: string[]; + private messageSubscription: Subscription|null = null; constructor(private room: Room, MapUrlFile: string, customKey?: string|undefined) { super({ @@ -293,25 +285,6 @@ export class GameScene extends ResizableScene implements CenterListener { } }); }); - - // import(/* webpackIgnore: true */ scriptUrl).then(result => { - // - // result.default.preload(this.load); - // - // this.load.start(); // Let's manually start the loader because the import might be over AFTER the loading ends. - // this.load.on('complete', () => { - // // FIXME: the factory might fail because the resources might not be loaded yet... - // // We would need to add a loader ended event in addition to the createPromise - // this.createPromise.then(() => { - // result.default.create(this); - // - // for (let object of objectsOfType) { - // // TODO: we should pass here a factory to create sprites (maybe?) - // let objectSprite = result.default.factory(this, object); - // } - // }); - // }); - // }); } } @@ -330,6 +303,8 @@ export class GameScene extends ResizableScene implements CenterListener { gameManager.gameSceneIsCreated(this); urlManager.pushRoomIdToUrl(this.room); this.startLayerName = urlManager.getStartLayerNameFromUrl(); + + this.messageSubscription = connectionManager._connexionMessageStream.subscribe((event) => this.onConnexionMessage(event)) const playerName = gameManager.getPlayerName(); if (!playerName) { @@ -401,13 +376,13 @@ export class GameScene extends ResizableScene implements CenterListener { this.scene.launch(ReconnectingSceneName); }, 0); } else if (this.connection === undefined) { - // Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking + // Let's wait 1 second before printing the "connecting" screen to avoid blinking setTimeout(() => { if (this.connection === undefined) { this.scene.sleep(); this.scene.launch(ReconnectingSceneName); } - }, 500); + }, 1000); } this.createPromiseResolve(); @@ -569,8 +544,6 @@ export class GameScene extends ResizableScene implements CenterListener { //init user position and play trigger to check layers properties this.gameMap.setPosition(this.CurrentPlayer.x, this.CurrentPlayer.y); - - return this.connection; }); } @@ -607,30 +580,7 @@ export class GameScene extends ResizableScene implements CenterListener { contextRed.stroke(); this.circleRedTexture.refresh(); } - - private playAudio(url: string|number|boolean|undefined, loop=false): void { - if (url === undefined) { - audioManager.unloadAudio(); - } else { - const audioPath = url as string; - let realAudioPath = ''; - - if (audioPath.indexOf('://') > 0) { - // remote file or stream - realAudioPath = audioPath; - } else { - // local file, include it relative to map directory - const mapDirUrl = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/')); - realAudioPath = mapDirUrl + '/' + url; - } - - audioManager.loadAudio(realAudioPath); - - if (loop) { - audioManager.loop(); - } - } - } + private safeParseJSONstring(jsonString: string|undefined, propertyName: string) { try { @@ -711,15 +661,19 @@ export class GameScene extends ResizableScene implements CenterListener { } }); this.gameMap.onPropertyChange('playAudio', (newValue, oldValue) => { - this.playAudio(newValue); + newValue === undefined ? audioManager.unloadAudio() : audioManager.playAudio(newValue, this.getMapDirUrl()); }); this.gameMap.onPropertyChange('playAudioLoop', (newValue, oldValue) => { - this.playAudio(newValue, true); + newValue === undefined ? audioManager.unloadAudio() : audioManager.playAudio(newValue, this.getMapDirUrl()); }); } + private getMapDirUrl(): string { + return this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/')); + } + private onMapExit(exitKey: string) { const {roomId, hash} = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance); if (!roomId) throw new Error('Could not find the room from its exit key: '+exitKey); @@ -745,14 +699,11 @@ export class GameScene extends ResizableScene implements CenterListener { // stop playing audio, close any open website, stop any open Jitsi coWebsiteManager.closeCoWebsite(); this.stopJitsi(); - this.playAudio(undefined); + audioManager.unloadAudio(); // We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map. - if(this.connection) { - this.connection.closeConnection(); - } - if(this.simplePeer) { - this.simplePeer.unregister(); - } + this.connection?.closeConnection(); + this.simplePeer?.unregister(); + this.messageSubscription?.unsubscribe(); } private removeAllRemotePlayers(): void { @@ -1267,21 +1218,34 @@ export class GameScene extends ResizableScene implements CenterListener { } public stopJitsi(): void { - this.connection.setSilent(false); + this.connection?.setSilent(false); jitsiFactory.stop(); mediaManager.showGameOverlay(); mediaManager.removeTriggerCloseJitsiFrameButton('close-jisi'); } + //todo: into onConnexionMessage private bannedUser(){ this.cleanupClosingScene(); this.userInputManager.clearAllKeys(); this.scene.start(ErrorSceneName, { title: 'Banned', - subTitle: 'You was banned of WorkAdventure', - message: 'If you want more information, you can contact us: workadventure@thecodingmachine.com' + subTitle: 'You were banned from WorkAdventure', + message: 'If you want more information, you may contact us at: workadventure@thecodingmachine.com' }); } + private onConnexionMessage(event: ConnexionMessageEvent) { + if (event.type === ConnexionMessageEventTypes.worldFull) { + this.cleanupClosingScene(); + this.scene.stop(ReconnectingSceneName); + this.userInputManager.clearAllKeys(); + this.scene.start(ErrorSceneName, { + title: 'Connection rejected', + subTitle: 'The world you are trying to join is full. Try again later.', + message: 'If you want more information, you may contact us at: workadventure@thecodingmachine.com' + }); + } + } } diff --git a/front/src/WebRtc/AudioManager.ts b/front/src/WebRtc/AudioManager.ts index 8ae3a9c0..24fb74a1 100644 --- a/front/src/WebRtc/AudioManager.ts +++ b/front/src/WebRtc/AudioManager.ts @@ -38,6 +38,25 @@ class AudioManager { HtmlUtils.getElementByIdOrFail('audioplayer_volume').value = '' + this.volume; } + public playAudio(url: string|number|boolean, mapDirUrl: string, loop=false): void { + const audioPath = url as string; + let realAudioPath = ''; + + if (audioPath.indexOf('://') > 0) { + // remote file or stream + realAudioPath = audioPath; + } else { + // local file, include it relative to map directory + realAudioPath = mapDirUrl + '/' + url; + } + + this.loadAudio(realAudioPath); + + if (loop) { + this.loop(); + } + } + private close(): void { this.audioPlayerCtrl.classList.remove('loading'); this.audioPlayerCtrl.classList.add('hidden'); @@ -75,7 +94,7 @@ class AudioManager { } - public loadAudio(url: string): void { + private loadAudio(url: string): void { this.load(); /* Solution 1, remove whole audio player */ @@ -125,7 +144,7 @@ class AudioManager { this.open(); } - public loop(): void { + private loop(): void { if (this.audioPlayerElem !== undefined) { this.audioPlayerElem.loop = true; } diff --git a/pusher/src/Controller/IoSocketController.ts b/pusher/src/Controller/IoSocketController.ts index 2391aaa3..38e3ef5b 100644 --- a/pusher/src/Controller/IoSocketController.ts +++ b/pusher/src/Controller/IoSocketController.ts @@ -1,5 +1,5 @@ import {CharacterLayer, ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." -import {GameRoomPolicyTypes} from "../Model/PusherRoom"; +import {GameRoomPolicyTypes, PusherRoom} from "../Model/PusherRoom"; import {PointInterface} from "../Model/Websocket/PointInterface"; import { SetPlayerDetailsMessage, @@ -20,7 +20,7 @@ import {parse} from "query-string"; import {jwtTokenManager} from "../Services/JWTTokenManager"; import {adminApi, CharacterTexture, FetchMemberDataByUuidResponse} from "../Services/AdminApi"; import {SocketManager, socketManager} from "../Services/SocketManager"; -import {emitInBatch} from "../Services/IoSocketHelpers"; +import {emitError, emitInBatch} from "../Services/IoSocketHelpers"; import {ADMIN_API_TOKEN, ADMIN_API_URL, SOCKET_IDLE_TIMER} from "../Enum/EnvironmentVariable"; import {Zone} from "_Model/Zone"; import {ExAdminSocketInterface} from "_Model/Websocket/ExAdminSocketInterface"; @@ -108,7 +108,6 @@ export class IoSocketController { maxBackpressure: 65536, // Maximum 64kB of data in the buffer. //idleTimeout: 10, upgrade: (res, req, context) => { - //console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!'); (async () => { /* Keep track of abortions */ const upgradeAborted = {aborted: false}; @@ -156,12 +155,9 @@ export class IoSocketController { const userUuid = await jwtTokenManager.getUserUuidFromToken(token, IPAddress, roomId); let memberTags: string[] = []; + let memberMessages: unknown; let memberTextures: CharacterTexture[] = []; const room = await socketManager.getOrCreateRoom(roomId); - // TODO: make sure the room isFull is ported in the back part. - /*if(room.isFull){ - throw new Error('Room is full'); - }*/ if (ADMIN_API_URL) { try { let userData : FetchMemberDataByUuidResponse = { @@ -172,15 +168,26 @@ export class IoSocketController { anonymous: true }; try { - userData = await adminApi.fetchMemberDataByUuid(userUuid); + userData = await adminApi.fetchMemberDataByUuid(userUuid, roomId); }catch (err){ if (err?.response?.status == 404) { // If we get an HTTP 404, the token is invalid. Let's perform an anonymous login! console.warn('Cannot find user with uuid "'+userUuid+'". Performing an anonymous login instead.'); + } else if(err?.response?.status == 403) { + // If we get an HTTP 404, the world is full. We need to broadcast a special error to the client. + // we finish immediatly the upgrade then we will close the socket as soon as it starts opening. + res.upgrade({ + rejected: true, + }, websocketKey, + websocketProtocol, + websocketExtensions, + context); + return; }else{ throw err; } } + memberMessages = userData.messages; memberTags = userData.tags; memberTextures = userData.textures; if (!room.anonymous && room.policyType === GameRoomPolicyTypes.USE_TAGS_POLICY && (userData.anonymous === true || !room.canAccess(memberTags))) { @@ -215,6 +222,7 @@ export class IoSocketController { roomId, name, characterLayers: characterLayerObjs, + messages: memberMessages, tags: memberTags, textures: memberTextures, position: { @@ -241,7 +249,6 @@ export class IoSocketController { console.log(e.message); res.writeStatus("401 Unauthorized").end(e.message); } else { - console.log(e); res.writeStatus("500 Internal Server Error").end('An error occurred'); } return; @@ -250,32 +257,30 @@ export class IoSocketController { }, /* Handlers */ open: (ws) => { + if(ws.rejected === true) { + emitError(ws, 'World is full'); + ws.close(); + } + // Let's join the room const client = this.initClient(ws); //todo: into the upgrade instead? socketManager.handleJoinRoom(client); //get data information and show messages - if (ADMIN_API_URL) { - adminApi.fetchMemberDataByUuid(client.userUuid).then((res: FetchMemberDataByUuidResponse) => { - if (!res.messages) { - return; + if (client.messages && Array.isArray(client.messages)) { + client.messages.forEach((c: unknown) => { + const messageToSend = c as { type: string, message: string }; + + const sendUserMessage = new SendUserMessage(); + sendUserMessage.setType(messageToSend.type); + sendUserMessage.setMessage(messageToSend.message); + + const serverToClientMessage = new ServerToClientMessage(); + serverToClientMessage.setSendusermessage(sendUserMessage); + + if (!client.disconnecting) { + client.send(serverToClientMessage.serializeBinary().buffer, true); } - res.messages.forEach((c: unknown) => { - const messageToSend = c as { type: string, message: string }; - - const sendUserMessage = new SendUserMessage(); - sendUserMessage.setType(messageToSend.type); - sendUserMessage.setMessage(messageToSend.message); - - const serverToClientMessage = new ServerToClientMessage(); - serverToClientMessage.setSendusermessage(sendUserMessage); - - if (!client.disconnecting) { - client.send(serverToClientMessage.serializeBinary().buffer, true); - } - }); - }).catch((err) => { - console.error('fetchMemberDataByUuid => err', err); }); } }, @@ -340,6 +345,7 @@ export class IoSocketController { } client.disconnecting = false; + client.messages = ws.messages; client.name = ws.name; client.tags = ws.tags; client.textures = ws.textures; diff --git a/pusher/src/Model/Websocket/ExSocketInterface.ts b/pusher/src/Model/Websocket/ExSocketInterface.ts index 85cfad48..56e7e5ca 100644 --- a/pusher/src/Model/Websocket/ExSocketInterface.ts +++ b/pusher/src/Model/Websocket/ExSocketInterface.ts @@ -36,6 +36,7 @@ export interface ExSocketInterface extends WebSocket, Identificable { batchedMessages: BatchMessage; batchTimeout: NodeJS.Timeout|null; disconnecting: boolean, + messages: unknown, tags: string[], textures: CharacterTexture[], backConnection: BackConnection, diff --git a/pusher/src/Services/AdminApi.ts b/pusher/src/Services/AdminApi.ts index 06d9239c..d50e2a4f 100644 --- a/pusher/src/Services/AdminApi.ts +++ b/pusher/src/Services/AdminApi.ts @@ -58,12 +58,12 @@ class AdminApi { return res.data; } - async fetchMemberDataByUuid(uuid: string): Promise { + async fetchMemberDataByUuid(uuid: string, roomId: string): Promise { if (!ADMIN_API_URL) { return Promise.reject('No admin backoffice set!'); } - const res = await Axios.get(ADMIN_API_URL+'/api/membership/'+uuid, - { headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } + const res = await Axios.get(ADMIN_API_URL+'/api/room/access', + { params: {uuid, roomId}, headers: {"Authorization" : `${ADMIN_API_TOKEN}`} } ) return res.data; } diff --git a/pusher/src/Services/IoSocketHelpers.ts b/pusher/src/Services/IoSocketHelpers.ts index 9c27c59a..e90e0874 100644 --- a/pusher/src/Services/IoSocketHelpers.ts +++ b/pusher/src/Services/IoSocketHelpers.ts @@ -1,5 +1,6 @@ import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import {BatchMessage, ErrorMessage, ServerToClientMessage, SubMessage} from "../Messages/generated/messages_pb"; +import {WebSocket} from "uWebSockets.js"; export function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void { socket.batchedMessages.addPayload(payload); @@ -20,7 +21,7 @@ export function emitInBatch(socket: ExSocketInterface, payload: SubMessage): voi } } -export function emitError(Client: ExSocketInterface, message: string): void { +export function emitError(Client: WebSocket, message: string): void { const errorMessage = new ErrorMessage(); errorMessage.setMessage(message); diff --git a/pusher/src/Services/SocketManager.ts b/pusher/src/Services/SocketManager.ts index 5c1f25ed..77a60f66 100644 --- a/pusher/src/Services/SocketManager.ts +++ b/pusher/src/Services/SocketManager.ts @@ -4,7 +4,6 @@ import { GroupDeleteMessage, ItemEventMessage, PlayGlobalMessage, - PositionMessage, RoomJoinedMessage, ServerToClientMessage, SetPlayerDetailsMessage, @@ -25,21 +24,15 @@ import { SendUserMessage, BanUserMessage, UserJoinedRoomMessage, UserLeftRoomMessage } from "../Messages/generated/messages_pb"; -import {PointInterface} from "../Model/Websocket/PointInterface"; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils"; -import {cpuTracker} from "./CpuTracker"; -import {GROUP_RADIUS, JITSI_ISS, MINIMUM_DISTANCE, SECRET_JITSI_KEY} from "../Enum/EnvironmentVariable"; -import {Movable} from "../Model/Movable"; -import {PositionInterface} from "../Model/PositionInterface"; +import {JITSI_ISS, SECRET_JITSI_KEY} from "../Enum/EnvironmentVariable"; import {adminApi, CharacterTexture} from "./AdminApi"; -import Direction = PositionMessage.Direction; import {emitError, emitInBatch} from "./IoSocketHelpers"; import Jwt from "jsonwebtoken"; import {JITSI_URL} from "../Enum/EnvironmentVariable"; import {clientEventsEmitter} from "./ClientEventsEmitter"; import {gaugeManager} from "./GaugeManager"; import {apiClientRepository} from "./ApiClientRepository"; -import {ServiceError} from "grpc"; import {GroupDescriptor, UserDescriptor, ZoneEventListener} from "_Model/Zone"; import Debug from "debug"; import {ExAdminSocketInterface} from "_Model/Websocket/ExAdminSocketInterface"; @@ -408,17 +401,7 @@ export class SocketManager implements ZoneEventListener { //check and create new world for a room let world = this.Worlds.get(roomId) if(world === undefined){ - world = new PusherRoom( - roomId, - this -/* (user: User, group: Group) => this.joinWebRtcRoom(user, group), - (user: User, group: Group) => this.disConnectedUser(user, group), - MINIMUM_DISTANCE, - GROUP_RADIUS, - (thing: Movable, listener: User) => this.onRoomEnter(thing, listener), - (thing: Movable, position:PositionInterface, listener:User) => this.onClientMove(thing, position, listener), - (thing: Movable, listener:User) => this.onClientLeave(thing, listener)*/ - ); + world = new PusherRoom(roomId, this); if (!world.anonymous) { const data = await adminApi.fetchMapDetails(world.organizationSlug, world.worldSlug, world.roomSlug) world.tags = data.tags @@ -429,60 +412,6 @@ export class SocketManager implements ZoneEventListener { return Promise.resolve(world) } -/* private joinRoom(client : ExSocketInterface, position: PointInterface): PusherRoom { - - const roomId = client.roomId; - client.position = position; - - const world = this.Worlds.get(roomId) - if(world === undefined){ - throw new Error('Could not find room for ID: '+client.roomId) - } - - // Dispatch groups position to newly connected user - world.getGroups().forEach((group: Group) => { - this.emitCreateUpdateGroupEvent(client, group); - }); - //join world - world.join(client, client.position); - clientEventsEmitter.emitClientJoin(client.userUuid, client.roomId); - console.log(new Date().toISOString() + ' A user joined (', this.sockets.size, ' connected users)'); - return world; - } - - private onClientMove(thing: Movable, position:PositionInterface, listener:User): void { - const clientListener = this.searchClientByIdOrFail(listener.id); - if (thing instanceof User) { - const clientUser = this.searchClientByIdOrFail(thing.id); - - const userMovedMessage = new UserMovedMessage(); - userMovedMessage.setUserid(clientUser.userId); - userMovedMessage.setPosition(ProtobufUtils.toPositionMessage(clientUser.position)); - - const subMessage = new SubMessage(); - subMessage.setUsermovedmessage(userMovedMessage); - - clientListener.emitInBatch(subMessage); - //console.log("Sending USER_MOVED event"); - } else if (thing instanceof Group) { - this.emitCreateUpdateGroupEvent(clientListener, thing); - } else { - console.error('Unexpected type for Movable.'); - } - } - - private onClientLeave(thing: Movable, listener:User) { - const clientListener = this.searchClientByIdOrFail(listener.id); - if (thing instanceof User) { - const clientUser = this.searchClientByIdOrFail(thing.id); - this.emitUserLeftEvent(clientListener, clientUser.userId); - } else if (thing instanceof Group) { - this.emitDeleteGroupEvent(clientListener, thing.getId()); - } else { - console.error('Unexpected type for Movable.'); - } - }*/ - emitPlayGlobalMessage(client: ExSocketInterface, playglobalmessage: PlayGlobalMessage) { const pusherToBackMessage = new PusherToBackMessage(); pusherToBackMessage.setPlayglobalmessage(playglobalmessage);