From b4f77ba51ac5a2f66f39787dc75e0045ab8eadfb Mon Sep 17 00:00:00 2001 From: gparant Date: Sun, 5 Apr 2020 15:51:47 +0200 Subject: [PATCH] Refactor to create interface rooms. The ExtRooms permit to refresh position of all users in the map and create data to share. --- back/src/Controller/IoSocketController.ts | 55 +++++++++---------- back/src/Model/Websocket/ExtRoom.ts | 42 ++++++++++++++ back/src/Model/Websocket/ExtRoomsInterface.ts | 6 ++ 3 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 back/src/Model/Websocket/ExtRoom.ts create mode 100644 back/src/Model/Websocket/ExtRoomsInterface.ts diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index ad305395..aa5dfdc9 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -5,6 +5,8 @@ import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TO import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.." import Jwt, {JsonWebTokenError} from "jsonwebtoken"; import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..." +import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"; +import {ExtRoomsInterface} from "_Model/Websocket/ExtRoomsInterface"; export class IoSocketController{ Io: socketIO.Server; @@ -44,21 +46,33 @@ export class IoSocketController{ if(messageUserPosition instanceof Error){ return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message})) } + //join user in room socket.join(messageUserPosition.roomId); + // sending to all clients in room except sender this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); + + //add function to refresh position user in real time. + let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface); + rooms.refreshUserPosition = RefreshUserPositionFunction; + rooms.refreshUserPosition(rooms, this.Io); + socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString()); }); socket.on('user-position', (message : string) => { let messageUserPosition = this.hydrateMessageReceive(message); - if(messageUserPosition instanceof Error){ + if (messageUserPosition instanceof Error) { return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message})); } + // sending to all clients in room except sender this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); - socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString()); + + //refresh position of all user in all rooms in real time + let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface) + rooms.refreshUserPosition(rooms, this.Io); }); }); } @@ -97,37 +111,18 @@ export class IoSocketController{ **/ seTimeOutInProgress : any = null; shareUsersPosition(){ - if(!this.seTimeOutInProgress) { + if(this.seTimeOutInProgress){ clearTimeout(this.seTimeOutInProgress); } - let clients = this.Io.clients(); - let socketsKey = Object.keys(this.Io.clients().sockets); - - //create mapping with all users in all rooms - let mapPositionUserByRoom = new Map(); - for(let i = 0; i < socketsKey.length; i++){ - let socket = clients.sockets[socketsKey[i]]; - if(!(socket as ExSocketInterface).position){ - continue; - } - let data = { - userId : (socket as ExSocketInterface).userId, - roomId : (socket as ExSocketInterface).roomId, - position : (socket as ExSocketInterface).position, - }; - let dataArray = []; - if(mapPositionUserByRoom.get(data.roomId)){ - dataArray = mapPositionUserByRoom.get(data.roomId); - dataArray.push(data); - }else{ - dataArray = [data]; - } - mapPositionUserByRoom.set(data.roomId, dataArray); - } - //send for each room, all data of position user - let arrayMap = Array.from(mapPositionUserByRoom); - arrayMap.forEach((value) => { + let arrayMap = (this.Io.sockets.adapter.rooms as ExtRooms).userPositionMapByRoom; + if(!arrayMap){ + this.seTimeOutInProgress = setTimeout(() => { + this.shareUsersPosition(); + }, 10); + return; + } + arrayMap.forEach((value : any) => { let roomId = value[0]; let data = value[1]; this.Io.in(roomId).emit('user-position', JSON.stringify(data)); diff --git a/back/src/Model/Websocket/ExtRoom.ts b/back/src/Model/Websocket/ExtRoom.ts new file mode 100644 index 00000000..03eb5e33 --- /dev/null +++ b/back/src/Model/Websocket/ExtRoom.ts @@ -0,0 +1,42 @@ +import {ExtRoomsInterface} from "./ExtRoomsInterface"; +import socketIO = require('socket.io'); +import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; + +export class ExtRooms implements ExtRoomsInterface{ + userPositionMapByRoom: any; + refreshUserPosition: any; + + [room: string]: SocketIO.Room; +} + +let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server){ + let clients = Io.clients(); + let socketsKey = Object.keys(Io.clients().sockets); + + //create mapping with all users in all rooms + let mapPositionUserByRoom = new Map(); + for(let i = 0; i < socketsKey.length; i++){ + let socket = clients.sockets[socketsKey[i]]; + if(!(socket as ExSocketInterface).position){ + continue; + } + let data = { + userId : (socket as ExSocketInterface).userId, + roomId : (socket as ExSocketInterface).roomId, + position : (socket as ExSocketInterface).position, + }; + let dataArray = []; + if(mapPositionUserByRoom.get(data.roomId)){ + dataArray = mapPositionUserByRoom.get(data.roomId); + dataArray.push(data); + }else{ + dataArray = [data]; + } + mapPositionUserByRoom.set(data.roomId, dataArray); + } + rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom); +} + +export { + RefreshUserPositionFunction +} \ No newline at end of file diff --git a/back/src/Model/Websocket/ExtRoomsInterface.ts b/back/src/Model/Websocket/ExtRoomsInterface.ts new file mode 100644 index 00000000..a71fcbf9 --- /dev/null +++ b/back/src/Model/Websocket/ExtRoomsInterface.ts @@ -0,0 +1,6 @@ +import {Rooms} from "socket.io"; + +export interface ExtRoomsInterface extends Rooms{ + userPositionMapByRoom: any; + refreshUserPosition: any; +} \ No newline at end of file