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/..."
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",
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;
}
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);
//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);
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;
}
delete Client.webRtcRoomId;
}
- /**
- *
- * @param Client
- */
leaveRoom(Client : ExSocketInterface){
//lease previous room and world
if(Client.roomId){
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) => {
}, (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
});
});
//join world
- world.join(Client, messageUserPosition);
- this.Worlds.set(messageUserPosition.roomId, world);
+ world.join(Client, Client.position);
+ this.Worlds.set(roomId, world);
}
-
-
}
/**
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);
}
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);
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:
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: "bar" }, new MessageUserPosition({
- userId: "barbar",
- roomId: 1,
- position: new Point(500, 100)
- }));
-
- world.updatePosition({ id: "bar" }, new MessageUserPosition({
- userId: "barbar",
- roomId: 1,
- position: new Point(261, 100)
- }));
+ world.join({ id: "foo" }, new Point(100, 100));
+
+ world.join({ id: "bar" }, new Point(500, 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);
});
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);
});
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);
});
GameManager: GameManager;
lastPositionShared: MessageUserPosition = null;
+ lastRoom: string|null = null;
constructor(GameManager: GameManager) {
this.GameManager = GameManager;
*/
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(
* @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;
}
/**