return new Error(err);
}
}
- }
+
+ /** permit to share user position
+ ** users position will send in event 'user-position'
+ ** The data sent is an array with information for each user :
+ [
+ {
+ userId: <string>,
+ roomId: <string>,
+ position: {
+ x : <number>,
+ y : <number>
+ }
+ },
+ ...
+ ]
+ **/
+ seTimeOutInProgress : any = null;
+ shareUsersPosition(){
+ 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 = <any>[];
+ 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 roomId = value[0];
+ let data = value[1];
+ this.Io.in(roomId).emit('user-position', JSON.stringify(data));
+ });
+ this.seTimeOutInProgress = setTimeout(() => {
+ this.shareUsersPosition();
+ }, 10);
+ }
+ }