Change to update world by scene and room id

This commit is contained in:
gparant 2020-05-10 19:45:17 +02:00
parent 58b65030bd
commit 8a91190d8c
2 changed files with 55 additions and 25 deletions

View File

@ -5,7 +5,7 @@ import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TO
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import Jwt, {JsonWebTokenError} from "jsonwebtoken"; import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import {SECRET_KEY, MINIMUM_DISTANCE, GROUP_RADIUS} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..." import {SECRET_KEY, MINIMUM_DISTANCE, GROUP_RADIUS} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"; import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRooms";
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface"; import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World"; import {World} from "../Model/World";
import {Group} from "_Model/Group"; import {Group} from "_Model/Group";
@ -27,7 +27,7 @@ enum SockerIoEvent {
export class IoSocketController { export class IoSocketController {
Io: socketIO.Server; Io: socketIO.Server;
World: World; Worlds: Map<string, World> = new Map<string, World>();
constructor(server: http.Server) { constructor(server: http.Server) {
this.Io = socketIO(server); this.Io = socketIO(server);
@ -48,17 +48,6 @@ export class IoSocketController {
this.ioConnection(); this.ioConnection();
this.shareUsersPosition(); this.shareUsersPosition();
//don't send only function because the context will be not this
this.World = new World((user1: string, group: Group) => {
this.connectedUser(user1, group);
}, (user1: string, group: Group) => {
this.disConnectedUser(user1, group);
}, MINIMUM_DISTANCE, GROUP_RADIUS, (group: Group) => {
this.sendUpdateGroupEvent(group);
}, (groupUuid: string, lastUser: UserInterface) => {
this.sendDeleteGroupEvent(groupUuid, lastUser);
});
} }
private sendUpdateGroupEvent(group: Group): void { private sendUpdateGroupEvent(group: Group): void {
@ -108,16 +97,42 @@ export class IoSocketController {
return; return;
} }
//lease previous room //lease previous room and world
if((socket as ExSocketInterface).roomId){ if((socket as ExSocketInterface).roomId){
socket.leave((socket as ExSocketInterface).roomId); let Client = (socket as ExSocketInterface);
//user leave previous room
socket.leave(Client.roomId);
//user leave previous world
let world : World|undefined = this.Worlds.get(Client.roomId);
if(world){
world.leave(Client);
this.Worlds.set(Client.roomId, world);
}
} }
//join user in room //join user in room
socket.join(messageUserPosition.roomId); socket.join(messageUserPosition.roomId);
//join user in world //check and create new world for a room
this.World.join(messageUserPosition); if(!this.Worlds.get(messageUserPosition.roomId)){
let world = new World((user1: string, group: Group) => {
this.connectedUser(user1, group);
}, (user1: string, group: Group) => {
this.disConnectedUser(user1, group);
}, MINIMUM_DISTANCE, GROUP_RADIUS, (group: Group) => {
this.sendUpdateGroupEvent(group);
}, (groupUuid: string, lastUser: UserInterface) => {
this.sendDeleteGroupEvent(groupUuid, lastUser);
});
this.Worlds.set(messageUserPosition.roomId, world);
}
//join world
let world : World|undefined = this.Worlds.get(messageUserPosition.roomId);
if(world) {
world.join(messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}
// sending to all clients in room except sender // sending to all clients in room except sender
this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
@ -125,6 +140,9 @@ export class IoSocketController {
//add function to refresh position user in real time. //add function to refresh position user in real time.
this.refreshUserPosition(); this.refreshUserPosition();
//refresh position in world
this.refreshWorldPosition(messageUserPosition);
socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString()); socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString());
}); });
@ -139,6 +157,9 @@ export class IoSocketController {
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
this.refreshUserPosition(); this.refreshUserPosition();
//refresh position in world
this.refreshWorldPosition(messageUserPosition);
}); });
socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => { socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => {
@ -171,8 +192,11 @@ export class IoSocketController {
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
this.refreshUserPosition(); this.refreshUserPosition();
//leave group of user let world : World|undefined = this.Worlds.get(Client.roomId);
this.World.leave(Client); if(world){
world.leave(Client);
this.Worlds.set(Client.roomId, world);
}
//leave room //leave room
socket.leave(Client.roomId); socket.leave(Client.roomId);
@ -272,7 +296,16 @@ export class IoSocketController {
if (!rooms.refreshUserPosition) { if (!rooms.refreshUserPosition) {
rooms.refreshUserPosition = RefreshUserPositionFunction; rooms.refreshUserPosition = RefreshUserPositionFunction;
} }
rooms.refreshUserPosition(rooms, this.Io, this.World); rooms.refreshUserPosition(rooms, this.Io);
}
refreshWorldPosition(messageUserPosition : MessageUserPosition){
// update position in the worl
let world = this.Worlds.get(messageUserPosition.roomId);
if(world) {
world.updatePosition(messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}
} }
//Hydrate and manage error //Hydrate and manage error

View File

@ -7,11 +7,12 @@ import {World} from "_Model/World";
export class ExtRooms implements ExtRoomsInterface{ export class ExtRooms implements ExtRoomsInterface{
userPositionMapByRoom: any; userPositionMapByRoom: any;
refreshUserPosition: any; refreshUserPosition: any;
Worlds: any;
[room: string]: SocketIO.Room; [room: string]: SocketIO.Room;
} }
let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server, World : World) { let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server) {
let clients = Io.clients(); let clients = Io.clients();
let socketsKey = Object.keys(Io.clients().sockets); let socketsKey = Object.keys(Io.clients().sockets);
@ -37,10 +38,6 @@ let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server
dataArray = [data]; dataArray = [data];
} }
mapPositionUserByRoom.set(data.roomId, dataArray); mapPositionUserByRoom.set(data.roomId, dataArray);
// update position in the worl
let messageUserPosition = new MessageUserPosition(data);
World.updatePosition(messageUserPosition);
} }
rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom); rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom);
} }