Refactor leave and join room

This commit is contained in:
gparant 2020-05-10 19:54:41 +02:00
parent 8a91190d8c
commit 8b9c36e3be
2 changed files with 68 additions and 60 deletions

View File

@ -93,56 +93,24 @@ export class IoSocketController {
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message})) return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}))
} }
if((socket as ExSocketInterface).roomId === messageUserPosition.roomId){ let Client = (socket as ExSocketInterface);
if(Client.roomId === messageUserPosition.roomId){
return; return;
} }
//lease previous room and world //leave previous room
if((socket as ExSocketInterface).roomId){ this.leaveRoom(Client);
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 new previous room
socket.join(messageUserPosition.roomId); this.joinRoom(Client, messageUserPosition);
//check and create new world for a room
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(Client, messageUserPosition);
//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());
}); });
@ -157,9 +125,6 @@ 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) => {
@ -192,14 +157,10 @@ 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();
let world : World|undefined = this.Worlds.get(Client.roomId);
if(world){
world.leave(Client);
this.Worlds.set(Client.roomId, world);
}
//leave room //leave room
socket.leave(Client.roomId); this.leaveRoom(Client);
//leave webrtc room
socket.leave(Client.webRtcRoomId); socket.leave(Client.webRtcRoomId);
//delete all socket information //delete all socket information
@ -245,6 +206,54 @@ export class IoSocketController {
delete Client.webRtcRoomId; delete Client.webRtcRoomId;
} }
/**
*
* @param Client
*/
leaveRoom(Client : ExSocketInterface){
//lease previous room and world
if(Client.roomId){
//user leave previous room
Client.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);
}
}
}
/**
*
* @param Client
* @param messageUserPosition
*/
joinRoom(Client : ExSocketInterface, messageUserPosition: MessageUserPosition){
//join user in room
Client.join(messageUserPosition.roomId);
//check and create new world for a room
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);
}
}
/** /**
* *
* @param socket * @param socket
@ -299,15 +308,6 @@ export class IoSocketController {
rooms.refreshUserPosition(rooms, this.Io); 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
hydrateMessageReceive(message: string): MessageUserPosition | Error { hydrateMessageReceive(message: string): MessageUserPosition | Error {
try { try {

View File

@ -12,7 +12,7 @@ export class ExtRooms implements ExtRoomsInterface{
[room: string]: SocketIO.Room; [room: string]: SocketIO.Room;
} }
let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server) { let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server, Worlds: Map<string, World>) {
let clients = Io.clients(); let clients = Io.clients();
let socketsKey = Object.keys(Io.clients().sockets); let socketsKey = Object.keys(Io.clients().sockets);
@ -38,6 +38,14 @@ 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);
let world = Worlds.get(messageUserPosition.roomId);
if(world) {
world.updatePosition(messageUserPosition);
Worlds.set(messageUserPosition.roomId, world);
}
} }
rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom); rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom);
} }