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

View file

@ -48,10 +48,10 @@ export class World {
return this.groups;
}
public join(socket : Identificable, userPosition: MessageUserPosition): void {
public join(socket : Identificable, userPosition: PointInterface): void {
this.users.set(socket.id, {
id: socket.id,
position: userPosition.position
position: userPosition
});
// Let's call update position to trigger the join / leave room
this.updatePosition(socket, userPosition);
@ -69,14 +69,14 @@ export class World {
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);
if(typeof user === 'undefined') {
return;
}
user.position.x = userPosition.position.x;
user.position.y = userPosition.position.y;
user.position.x = userPosition.x;
user.position.y = userPosition.y;
if (typeof user.group === 'undefined') {
// 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, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "foo" }, new Point(100, 100));
world.join({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(500, 100)
}));
world.join({ id: "bar" }, new Point(500, 100));
world.updatePosition({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(261, 100)
}));
world.updatePosition({ id: "bar" }, new Point(261, 100));
expect(connectCalledNumber).toBe(0);
world.updatePosition({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(101, 100)
}));
world.updatePosition({ id: "bar" }, new Point(101, 100));
expect(connectCalledNumber).toBe(2);
world.updatePosition({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(102, 100)
}));
world.updatePosition({ id: "bar" }, new Point(102, 100));
expect(connectCalledNumber).toBe(2);
});
@ -64,35 +44,19 @@ describe("World", () => {
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "foo" }, new Point(100, 100));
world.join({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(200, 100)
}));
world.join({ id: "bar" }, new Point(200, 100));
expect(connectCalled).toBe(true);
connectCalled = false;
// baz joins at the outer limit of the group
world.join({ id: "baz" }, new MessageUserPosition({
userId: "bazbaz",
roomId: 1,
position: new Point(311, 100)
}));
world.join({ id: "baz" }, new Point(311, 100));
expect(connectCalled).toBe(false);
world.updatePosition({ id: "baz" }, new MessageUserPosition({
userId: "bazbaz",
roomId: 1,
position: new Point(309, 100)
}));
world.updatePosition({ id: "baz" }, new Point(309, 100));
expect(connectCalled).toBe(true);
});
@ -109,34 +73,18 @@ describe("World", () => {
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
world.join({ id: "foo" }, new MessageUserPosition({
userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
world.join({ id: "foo" }, new Point(100, 100));
world.join({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(259, 100)
}));
world.join({ id: "bar" }, new Point(259, 100));
expect(connectCalled).toBe(true);
expect(disconnectCallNumber).toBe(0);
world.updatePosition({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(100+160+160+1, 100)
}));
world.updatePosition({ id: "bar" }, new Point(100+160+160+1, 100));
expect(disconnectCallNumber).toBe(2);
world.updatePosition({ id: "bar" }, new MessageUserPosition({
userId: "barbar",
roomId: 1,
position: new Point(262, 100)
}));
world.updatePosition({ id: "bar" }, new Point(262, 100));
expect(disconnectCallNumber).toBe(2);
});

View file

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