JoinRoom now ONLY sends the roomId.

This commit is contained in:
David Négrier 2020-05-15 23:24:04 +02:00
parent 7e00d61f94
commit cdfa9acf01
4 changed files with 57 additions and 116 deletions

View file

@ -1,7 +1,7 @@
import socketIO = require('socket.io'); import socketIO = require('socket.io');
import {Socket} from "socket.io"; import {Socket} from "socket.io";
import * as http from "http"; import * as http from "http";
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.." import {MessageUserPosition, Point} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.."
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/..."
@ -11,6 +11,7 @@ import {World} from "../Model/World";
import {Group} from "_Model/Group"; import {Group} from "_Model/Group";
import {UserInterface} from "_Model/UserInterface"; import {UserInterface} from "_Model/UserInterface";
import {SetPlayerDetailsMessage} from "_Model/Websocket/SetPlayerDetailsMessage"; import {SetPlayerDetailsMessage} from "_Model/Websocket/SetPlayerDetailsMessage";
import {MessageUserPositionInterface} from "../../../front/src/Connexion";
enum SockerIoEvent { enum SockerIoEvent {
CONNECTION = "connection", CONNECTION = "connection",
@ -94,16 +95,15 @@ export class IoSocketController {
x: user x position on map x: user x position on map
y: user y position on map y: user y position on map
*/ */
socket.on(SockerIoEvent.JOIN_ROOM, (message: string) => { socket.on(SockerIoEvent.JOIN_ROOM, (roomId: any) => {
try { try {
let messageUserPosition = this.hydrateMessageReceive(message); if (typeof(roomId) !== 'string') {
if (messageUserPosition instanceof Error) { return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: 'Expected roomId as a string.'})
return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: messageUserPosition.message})
} }
let Client = (socket as ExSocketInterface); let Client = (socket as ExSocketInterface);
if (Client.roomId === messageUserPosition.roomId) { if (Client.roomId === roomId) {
return; return;
} }
@ -111,15 +111,20 @@ export class IoSocketController {
this.leaveRoom(Client); this.leaveRoom(Client);
//join new previous room //join new previous room
this.joinRoom(Client, messageUserPosition); this.joinRoom(Client, roomId);
// sending to all clients in room except sender
this.saveUserInformation(Client, messageUserPosition);
//add function to refresh position user in real time. //add function to refresh position user in real time.
this.refreshUserPosition(Client); this.refreshUserPosition(Client);
socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString()); let messageUserPosition = new MessageUserPosition({
userId: Client.id,
roomId: Client.roomId,
name: Client.name,
character: Client.character,
position: new Point(0, 0, 'none')
});
socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition);
} catch (e) { } catch (e) {
console.error('An error occurred on "join_room" event'); console.error('An error occurred on "join_room" event');
console.error(e); console.error(e);
@ -150,7 +155,7 @@ export class IoSocketController {
//send only at user //send only at user
let client = this.searchClientById(data.receiverId); let client = this.searchClientById(data.receiverId);
if (!client) { if (!client) {
console.error("client doesn't exist for ", data.receiverId); console.error("While exchanging a WebRTC signal: client doesn't exist for ", data.receiverId);
return; return;
} }
return client.emit(SockerIoEvent.WEBRTC_SIGNAL, data); return client.emit(SockerIoEvent.WEBRTC_SIGNAL, data);
@ -216,7 +221,7 @@ export class IoSocketController {
return client; return client;
} }
console.log("Could not find user with id ", userId); console.log("Could not find user with id ", userId);
throw new Error("Could not find user with id " + userId); //throw new Error("Could not find user with id " + userId);
return null; return null;
} }
@ -252,10 +257,6 @@ export class IoSocketController {
delete Client.webRtcRoomId; delete Client.webRtcRoomId;
} }
/**
*
* @param Client
*/
leaveRoom(Client : ExSocketInterface){ leaveRoom(Client : ExSocketInterface){
//lease previous room and world //lease previous room and world
if(Client.roomId){ if(Client.roomId){
@ -270,17 +271,15 @@ export class IoSocketController {
delete Client.roomId; delete Client.roomId;
} }
} }
/**
* joinRoom(Client : ExSocketInterface, roomId: string){
* @param Client
* @param messageUserPosition
*/
joinRoom(Client : ExSocketInterface, messageUserPosition: MessageUserPosition){
//join user in room //join user in room
Client.join(messageUserPosition.roomId); Client.join(roomId);
Client.roomId = roomId;
Client.position = new Point(-1000, -1000);
//check and create new world for a room //check and create new world for a room
if(!this.Worlds.get(messageUserPosition.roomId)){ if(!this.Worlds.get(roomId)){
let world = new World((user1: string, group: Group) => { let world = new World((user1: string, group: Group) => {
this.connectedUser(user1, group); this.connectedUser(user1, group);
}, (user1: string, group: Group) => { }, (user1: string, group: Group) => {
@ -290,11 +289,10 @@ export class IoSocketController {
}, (groupUuid: string, lastUser: UserInterface) => { }, (groupUuid: string, lastUser: UserInterface) => {
this.sendDeleteGroupEvent(groupUuid, lastUser); this.sendDeleteGroupEvent(groupUuid, lastUser);
}); });
this.Worlds.set(messageUserPosition.roomId, world); this.Worlds.set(roomId, world);
} }
let world : World|undefined = this.Worlds.get(messageUserPosition.roomId); let world : World|undefined = this.Worlds.get(roomId);
if(world) { if(world) {
// Dispatch groups position to newly connected user // Dispatch groups position to newly connected user
@ -305,11 +303,9 @@ export class IoSocketController {
}); });
}); });
//join world //join world
world.join(Client, messageUserPosition); world.join(Client, Client.position);
this.Worlds.set(messageUserPosition.roomId, world); this.Worlds.set(roomId, world);
} }
} }
/** /**
@ -373,13 +369,13 @@ export class IoSocketController {
position: Client.position, position: Client.position,
name: Client.name, name: Client.name,
character: Client.character, character: Client.character,
}; } as MessageUserPositionInterface;
let messageUserPosition = new MessageUserPosition(data); let messageUserPosition = new MessageUserPosition(data);
let world = this.Worlds.get(messageUserPosition.roomId); let world = this.Worlds.get(messageUserPosition.roomId);
if (!world) { if (!world) {
return; return;
} }
world.updatePosition(Client, messageUserPosition); world.updatePosition(Client, messageUserPosition.position);
this.Worlds.set(messageUserPosition.roomId, world); this.Worlds.set(messageUserPosition.roomId, world);
} }

View file

@ -48,10 +48,10 @@ export class World {
return this.groups; return this.groups;
} }
public join(socket : Identificable, userPosition: MessageUserPosition): void { public join(socket : Identificable, userPosition: PointInterface): void {
this.users.set(socket.id, { this.users.set(socket.id, {
id: socket.id, id: socket.id,
position: userPosition.position position: userPosition
}); });
// Let's call update position to trigger the join / leave room // Let's call update position to trigger the join / leave room
this.updatePosition(socket, userPosition); this.updatePosition(socket, userPosition);
@ -69,14 +69,14 @@ export class World {
this.users.delete(user.id); this.users.delete(user.id);
} }
public updatePosition(socket : Identificable, userPosition: MessageUserPosition): void { public updatePosition(socket : Identificable, userPosition: PointInterface): void {
let user = this.users.get(socket.id); let user = this.users.get(socket.id);
if(typeof user === 'undefined') { if(typeof user === 'undefined') {
return; return;
} }
user.position.x = userPosition.position.x; user.position.x = userPosition.x;
user.position.y = userPosition.position.y; user.position.y = userPosition.y;
if (typeof user.group === 'undefined') { if (typeof user.group === 'undefined') {
// If the user is not part of a group: // If the user is not part of a group:

View file

@ -17,39 +17,19 @@ describe("World", () => {
let world = new World(connect, disconnect, 160, 160, () => {}, () => {}); let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({ world.join({ id: "foo" }, new Point(100, 100));
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "bar" }, new MessageUserPosition({ world.join({ id: "bar" }, new Point(500, 100));
userId: "barbar",
roomId: 1,
position: new Point(500, 100)
}));
world.updatePosition({ id: "bar" }, new MessageUserPosition({ world.updatePosition({ id: "bar" }, new Point(261, 100));
userId: "barbar",
roomId: 1,
position: new Point(261, 100)
}));
expect(connectCalledNumber).toBe(0); expect(connectCalledNumber).toBe(0);
world.updatePosition({ id: "bar" }, new MessageUserPosition({ world.updatePosition({ id: "bar" }, new Point(101, 100));
userId: "barbar",
roomId: 1,
position: new Point(101, 100)
}));
expect(connectCalledNumber).toBe(2); expect(connectCalledNumber).toBe(2);
world.updatePosition({ id: "bar" }, new MessageUserPosition({ world.updatePosition({ id: "bar" }, new Point(102, 100));
userId: "barbar",
roomId: 1,
position: new Point(102, 100)
}));
expect(connectCalledNumber).toBe(2); expect(connectCalledNumber).toBe(2);
}); });
@ -64,35 +44,19 @@ describe("World", () => {
let world = new World(connect, disconnect, 160, 160, () => {}, () => {}); let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({ world.join({ id: "foo" }, new Point(100, 100));
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "bar" }, new MessageUserPosition({ world.join({ id: "bar" }, new Point(200, 100));
userId: "barbar",
roomId: 1,
position: new Point(200, 100)
}));
expect(connectCalled).toBe(true); expect(connectCalled).toBe(true);
connectCalled = false; connectCalled = false;
// baz joins at the outer limit of the group // baz joins at the outer limit of the group
world.join({ id: "baz" }, new MessageUserPosition({ world.join({ id: "baz" }, new Point(311, 100));
userId: "bazbaz",
roomId: 1,
position: new Point(311, 100)
}));
expect(connectCalled).toBe(false); expect(connectCalled).toBe(false);
world.updatePosition({ id: "baz" }, new MessageUserPosition({ world.updatePosition({ id: "baz" }, new Point(309, 100));
userId: "bazbaz",
roomId: 1,
position: new Point(309, 100)
}));
expect(connectCalled).toBe(true); expect(connectCalled).toBe(true);
}); });
@ -109,34 +73,18 @@ describe("World", () => {
let world = new World(connect, disconnect, 160, 160, () => {}, () => {}); let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({ world.join({ id: "foo" }, new Point(100, 100));
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "bar" }, new MessageUserPosition({ world.join({ id: "bar" }, new Point(259, 100));
userId: "barbar",
roomId: 1,
position: new Point(259, 100)
}));
expect(connectCalled).toBe(true); expect(connectCalled).toBe(true);
expect(disconnectCallNumber).toBe(0); expect(disconnectCallNumber).toBe(0);
world.updatePosition({ id: "bar" }, new MessageUserPosition({ world.updatePosition({ id: "bar" }, new Point(100+160+160+1, 100));
userId: "barbar",
roomId: 1,
position: new Point(100+160+160+1, 100)
}));
expect(disconnectCallNumber).toBe(2); expect(disconnectCallNumber).toBe(2);
world.updatePosition({ id: "bar" }, new MessageUserPosition({ world.updatePosition({ id: "bar" }, new Point(262, 100));
userId: "barbar",
roomId: 1,
position: new Point(262, 100)
}));
expect(disconnectCallNumber).toBe(2); expect(disconnectCallNumber).toBe(2);
}); });

View file

@ -150,6 +150,7 @@ export class Connexion implements ConnexionInterface {
GameManager: GameManager; GameManager: GameManager;
lastPositionShared: MessageUserPosition = null; lastPositionShared: MessageUserPosition = null;
lastRoom: string|null = null;
constructor(GameManager: GameManager) { constructor(GameManager: GameManager) {
this.GameManager = GameManager; this.GameManager = GameManager;
@ -185,12 +186,14 @@ export class Connexion implements ConnexionInterface {
*/ */
connectSocketServer(): Promise<ConnexionInterface>{ connectSocketServer(): Promise<ConnexionInterface>{
//if try to reconnect with last position //if try to reconnect with last position
if(this.lastPositionShared) { if(this.lastRoom) {
//join the room //join the room
this.joinARoom( this.joinARoom(
this.lastPositionShared.roomId, this.lastRoom
this.lastPositionShared.character
); );
}
if(this.lastPositionShared) {
//share your first position //share your first position
this.sharePosition( this.sharePosition(
@ -236,15 +239,9 @@ export class Connexion implements ConnexionInterface {
* @param roomId * @param roomId
* @param character * @param character
*/ */
joinARoom(roomId: string, character: string): void { joinARoom(roomId: string): void {
let messageUserPosition = new MessageUserPosition( this.socket.emit(EventMessage.JOIN_ROOM, roomId);
this.userId, this.lastRoom = roomId;
roomId,
new Point(0, 0),
this.name,
character
);
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
} }
/** /**