Merge pull request #118 from thecodingmachine/remove_userid

Refactoring messages
This commit is contained in:
David Négrier 2020-05-17 12:11:03 +02:00 committed by GitHub
commit ecd4c03b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 169 additions and 400 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/..."
@ -10,11 +10,11 @@ import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World"; 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";
enum SockerIoEvent { enum SockerIoEvent {
CONNECTION = "connection", CONNECTION = "connection",
DISCONNECT = "disconnect", DISCONNECT = "disconnect",
ATTRIBUTE_USER_ID = "attribute-user-id", // Sent from server to client just after the connexion is established to give the client its unique id.
JOIN_ROOM = "join-room", JOIN_ROOM = "join-room",
USER_POSITION = "user-position", USER_POSITION = "user-position",
WEBRTC_SIGNAL = "webrtc-signal", WEBRTC_SIGNAL = "webrtc-signal",
@ -24,6 +24,7 @@ enum SockerIoEvent {
MESSAGE_ERROR = "message-error", MESSAGE_ERROR = "message-error",
GROUP_CREATE_UPDATE = "group-create-update", GROUP_CREATE_UPDATE = "group-create-update",
GROUP_DELETE = "group-delete", GROUP_DELETE = "group-delete",
SET_PLAYER_DETAILS = "set-player-details"
} }
export class IoSocketController { export class IoSocketController {
@ -93,16 +94,16 @@ 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): void => {
try { try {
let messageUserPosition = this.hydrateMessageReceive(message); if (typeof(roomId) !== 'string') {
if (messageUserPosition instanceof Error) { socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: 'Expected roomId as a string.'});
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message})) return;
} }
let Client = (socket as ExSocketInterface); let Client = (socket as ExSocketInterface);
if (Client.roomId === messageUserPosition.roomId) { if (Client.roomId === roomId) {
return; return;
} }
@ -110,32 +111,32 @@ 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(Client.id, Client.name, Client.character,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);
} }
}); });
socket.on(SockerIoEvent.USER_POSITION, (message: string) => { socket.on(SockerIoEvent.USER_POSITION, (message: any): void => {
try { try {
let messageUserPosition = this.hydrateMessageReceive(message); let position = this.hydratePositionReceive(message);
if (messageUserPosition instanceof Error) { if (position instanceof Error) {
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message})); socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: position.message});
return;
} }
let Client = (socket as ExSocketInterface); let Client = (socket as ExSocketInterface);
// sending to all clients in room except sender // sending to all clients in room except sender
this.saveUserInformation(Client, messageUserPosition); Client.position = position;
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
this.refreshUserPosition(Client); this.refreshUserPosition(Client);
@ -145,19 +146,17 @@ export class IoSocketController {
} }
}); });
socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => { socket.on(SockerIoEvent.WEBRTC_SIGNAL, (data: any) => {
let data: any = JSON.parse(message);
//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, message); return client.emit(SockerIoEvent.WEBRTC_SIGNAL, data);
}); });
socket.on(SockerIoEvent.WEBRTC_OFFER, (message: string) => { socket.on(SockerIoEvent.WEBRTC_OFFER, (data: any) => {
let data: any = JSON.parse(message);
//send only at user //send only at user
let client = this.searchClientById(data.receiverId); let client = this.searchClientById(data.receiverId);
@ -165,7 +164,7 @@ export class IoSocketController {
console.error("client doesn't exist for ", data.receiverId); console.error("client doesn't exist for ", data.receiverId);
return; return;
} }
client.emit(SockerIoEvent.WEBRTC_OFFER, message); client.emit(SockerIoEvent.WEBRTC_OFFER, data);
}); });
socket.on(SockerIoEvent.DISCONNECT, () => { socket.on(SockerIoEvent.DISCONNECT, () => {
@ -194,7 +193,12 @@ export class IoSocketController {
}); });
// Let's send the user id to the user // Let's send the user id to the user
socket.emit(SockerIoEvent.ATTRIBUTE_USER_ID, socket.id); socket.on(SockerIoEvent.SET_PLAYER_DETAILS, (playerDetails: SetPlayerDetailsMessage, answerFn) => {
let Client = (socket as ExSocketInterface);
Client.name = playerDetails.name;
Client.character = playerDetails.character;
answerFn(socket.id);
});
}); });
} }
@ -212,6 +216,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);
return null; return null;
} }
@ -235,9 +240,9 @@ export class IoSocketController {
* @param Client: ExSocketInterface * @param Client: ExSocketInterface
*/ */
sendDisconnectedEvent(Client: ExSocketInterface) { sendDisconnectedEvent(Client: ExSocketInterface) {
Client.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, JSON.stringify({ Client.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, {
userId: Client.id userId: Client.id
})); });
//disconnect webrtc room //disconnect webrtc room
if(!Client.webRtcRoomId){ if(!Client.webRtcRoomId){
@ -247,17 +252,12 @@ 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){
//user leave previous world //user leave previous world
let world : World|undefined = this.Worlds.get(Client.roomId); let world : World|undefined = this.Worlds.get(Client.roomId);
if(world){ if(world){
console.log('Entering world.leave')
world.leave(Client); world.leave(Client);
//this.Worlds.set(Client.roomId, world); //this.Worlds.set(Client.roomId, world);
} }
@ -266,17 +266,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) => {
@ -286,11 +284,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
@ -301,11 +298,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);
} }
} }
/** /**
@ -341,19 +336,10 @@ export class IoSocketController {
return tabs; return tabs;
}, []); }, []);
client.emit(SockerIoEvent.WEBRTC_START, JSON.stringify({clients: clientsId, roomId: roomId})); client.emit(SockerIoEvent.WEBRTC_START, {clients: clientsId, roomId: roomId});
}); });
} }
//permit to save user position in socket
saveUserInformation(socket: ExSocketInterface, message: MessageUserPosition) {
socket.position = message.position;
socket.roomId = message.roomId;
//socket.userId = message.userId;
socket.name = message.name;
socket.character = message.character;
}
refreshUserPosition(Client : ExSocketInterface) { refreshUserPosition(Client : ExSocketInterface) {
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface); let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface);
@ -363,26 +349,22 @@ export class IoSocketController {
rooms.refreshUserPosition(rooms, this.Io); rooms.refreshUserPosition(rooms, this.Io);
// update position in the world // update position in the world
let data = { let messageUserPosition = new MessageUserPosition(Client.id, Client.name, Client.character, Client.position);
userId: Client.id, let world = this.Worlds.get(Client.roomId);
roomId: Client.roomId,
position: Client.position,
name: Client.name,
character: Client.character,
};
let messageUserPosition = new MessageUserPosition(data);
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(Client.roomId, world);
} }
//Hydrate and manage error //Hydrate and manage error
hydrateMessageReceive(message: string): MessageUserPosition | Error { hydratePositionReceive(message: any): Point | Error {
try { try {
return new MessageUserPosition(JSON.parse(message)); if (!message.x || !message.y || !message.direction) {
return new Error("invalid point message sent");
}
return new Point(message.x, message.y, message.direction);
} catch (err) { } catch (err) {
//TODO log error //TODO log error
return new Error(err); return new Error(err);
@ -421,7 +403,7 @@ export class IoSocketController {
} }
arrayMap.forEach((value: any) => { arrayMap.forEach((value: any) => {
let roomId = value[0]; let roomId = value[0];
this.Io.in(roomId).emit(SockerIoEvent.USER_POSITION, JSON.stringify(value)); this.Io.in(roomId).emit(SockerIoEvent.USER_POSITION, value);
}); });
this.seTimeOutInProgress = setTimeout(() => { this.seTimeOutInProgress = setTimeout(() => {
this.shareUsersPosition(); this.shareUsersPosition();

View File

@ -23,19 +23,18 @@ let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server
} }
let data = { let data = {
userId: socket.id, userId: socket.id,
roomId: socket.roomId,
position: socket.position, position: socket.position,
name: socket.name, name: socket.name,
character: socket.character, character: socket.character,
}; };
let dataArray = <any>[]; let dataArray = <any>[];
if (mapPositionUserByRoom.get(data.roomId)) { if (mapPositionUserByRoom.get(socket.roomId)) {
dataArray = mapPositionUserByRoom.get(data.roomId); dataArray = mapPositionUserByRoom.get(socket.roomId);
dataArray.push(data); dataArray.push(data);
} else { } else {
dataArray = [data]; dataArray = [data];
} }
mapPositionUserByRoom.set(data.roomId, dataArray); mapPositionUserByRoom.set(socket.roomId, dataArray);
} }
rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom); rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom);
}; };

View File

@ -1,27 +0,0 @@
export class Message {
userId: string;
roomId: string;
name: string;
character: string;
constructor(data: any) {
if (!data.userId || !data.roomId) {
console.error("Got invalid message", data);
throw Error("userId or roomId cannot be null");
}
this.userId = data.userId;
this.roomId = data.roomId;
this.name = data.name;
this.character = data.character;
}
toJson() {
return {
userId: this.userId,
roomId: this.roomId,
name: this.name,
character: this.character
}
}
}

View File

@ -1,44 +1,11 @@
import {Message} from "./Message";
import {PointInterface} from "./PointInterface"; import {PointInterface} from "./PointInterface";
export class Point implements PointInterface{ export class Point implements PointInterface{
x: number; constructor(public x : number, public y : number, public direction : string = "none") {
y: number;
direction: string;
constructor(x : number, y : number, direction : string = "none") {
if(x === null || y === null){
throw Error("position x and y cannot be null");
}
this.x = x;
this.y = y;
this.direction = direction;
}
toJson(){
return {
x : this.x,
y: this.y,
direction: this.direction
}
} }
} }
export class MessageUserPosition extends Message{ export class MessageUserPosition {
position: PointInterface; constructor(public userId: string, public name: string, public character: string, public position: PointInterface) {
constructor(message: any) {
super(message);
this.position = new Point(message.position.x, message.position.y, message.position.direction);
}
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
} }
} }

View File

@ -2,5 +2,4 @@ export interface PointInterface {
x: number; x: number;
y: number; y: number;
direction: string; direction: string;
toJson() : object;
} }

View File

@ -0,0 +1,4 @@
export interface SetPlayerDetailsMessage {
name: string,
character: string
}

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

@ -1,40 +0,0 @@
import "jasmine";
import {Message} from "../src/Model/Websocket/Message";
describe("Message Model", () => {
it("should find userId and roomId", () => {
let message = {userId: "test1", roomId: "test2", name: "foo", character: "user"};
let messageObject = new Message(message);
expect(messageObject.userId).toBe("test1");
expect(messageObject.roomId).toBe("test2");
expect(messageObject.name).toBe("foo");
expect(messageObject.character).toBe("user");
})
it("should expose a toJson method", () => {
let message = {userId: "test1", roomId: "test2", name: "foo", character: "user"};
let messageObject = new Message(message);
expect(messageObject.toJson()).toEqual({userId: "test1", roomId: "test2", name: "foo", character: "user"});
});
it("should find throw error when no userId", () => {
let message = {roomId: "test2"};
expect(() => {
let messageObject = new Message(message);
}).toThrow(new Error("userId or roomId cannot be null"));
});
it("should find throw error when no roomId", () => {
let message = {userId: "test1"};
expect(() => {
let messageObject = new Message(message);
}).toThrow(new Error("userId or roomId cannot be null"));
});
it("should find throw error when no roomId", () => {
let message = {name: "foo"};
expect(() => {
let messageObject = new Message(message);
}).toThrow(new Error("userId or roomId cannot be null"));
});
})

View File

@ -1,98 +1,60 @@
import "jasmine"; import "jasmine";
import {Message} from "../src/Model/Websocket/Message";
import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World"; import {World, ConnectCallback, DisconnectCallback } from "../src/Model/World";
import {MessageUserPosition, Point} from "../src/Model/Websocket/MessageUserPosition"; import {Point} from "../src/Model/Websocket/MessageUserPosition";
import { Group } from "../src/Model/Group"; import { Group } from "../src/Model/Group";
import {Distance} from "../src/Model//Distance";
describe("World", () => { describe("World", () => {
it("should connect user1 and user2", () => { it("should connect user1 and user2", () => {
let connectCalledNumber: number = 0; let connectCalledNumber: number = 0;
let connect = (user: string, group: Group): void => { let connect: ConnectCallback = (user: string, group: Group): void => {
connectCalledNumber++; connectCalledNumber++;
} }
let disconnect = (user: string, group: Group): void => { let disconnect: DisconnectCallback = (user: string, group: Group): void => {
} }
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);
}); });
it("should connect 3 users", () => { it("should connect 3 users", () => {
let connectCalled: boolean = false; let connectCalled: boolean = false;
let connect = (user: string, group: Group): void => { let connect: ConnectCallback = (user: string, group: Group): void => {
connectCalled = true; connectCalled = true;
} }
let disconnect = (user: string, group: Group): void => { let disconnect: DisconnectCallback = (user: string, group: Group): void => {
} }
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);
}); });
@ -100,43 +62,27 @@ describe("World", () => {
it("should disconnect user1 and user2", () => { it("should disconnect user1 and user2", () => {
let connectCalled: boolean = false; let connectCalled: boolean = false;
let disconnectCallNumber: number = 0; let disconnectCallNumber: number = 0;
let connect = (user: string, group: Group): void => { let connect: ConnectCallback = (user: string, group: Group): void => {
connectCalled = true; connectCalled = true;
} }
let disconnect = (user: string, group: Group): void => { let disconnect: DisconnectCallback = (user: string, group: Group): void => {
disconnectCallNumber++; disconnectCallNumber++;
} }
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

@ -36,8 +36,8 @@
/* Additional Checks */ /* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */ // "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */ // "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */ /* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */

View File

@ -1,10 +1,12 @@
import {GameManager} from "./Phaser/Game/GameManager"; import {GameManager} from "./Phaser/Game/GameManager";
const SocketIo = require('socket.io-client');
import Axios from "axios"; import Axios from "axios";
import {API_URL} from "./Enum/EnvironmentVariable"; import {API_URL} from "./Enum/EnvironmentVariable";
import {getMapKeyByUrl} from "./Phaser/Login/LogincScene";
import {MessageUI} from "./Logger/MessageUI"; import {MessageUI} from "./Logger/MessageUI";
import {SetPlayerDetailsMessage} from "./Messages/SetPlayerDetailsMessage";
const SocketIo = require('socket.io-client');
import Socket = SocketIOClient.Socket;
enum EventMessage{ enum EventMessage{
WEBRTC_SIGNAL = "webrtc-signal", WEBRTC_SIGNAL = "webrtc-signal",
@ -19,37 +21,25 @@ enum EventMessage{
CONNECT_ERROR = "connect_error", CONNECT_ERROR = "connect_error",
RECONNECT = "reconnect", RECONNECT = "reconnect",
ATTRIBUTE_USER_ID = "attribute-user-id" // Sent from server to client just after the connexion is established to give the client its unique id. SET_PLAYER_DETAILS = "set-player-details" // Send the name and character to the server (on connect), receive back the id.
} }
class Message { class Message {
userId: string; userId: string;
roomId: string;
name: string; name: string;
character: string; character: string;
constructor(userId : string, roomId : string, name: string, character: string) { constructor(userId : string, name: string, character: string) {
this.userId = userId; this.userId = userId;
this.roomId = roomId;
this.name = name; this.name = name;
this.character = character; this.character = character;
} }
toJson() {
return {
userId: this.userId,
roomId: this.roomId,
name: this.name,
character: this.character
}
}
} }
export interface PointInterface { export interface PointInterface {
x: number; x: number;
y: number; y: number;
direction : string; direction : string;
toJson() : object;
} }
class Point implements PointInterface{ class Point implements PointInterface{
@ -65,19 +55,10 @@ class Point implements PointInterface{
this.y = y; this.y = y;
this.direction = direction; this.direction = direction;
} }
toJson(){
return {
x : this.x,
y: this.y,
direction: this.direction
}
}
} }
export interface MessageUserPositionInterface { export interface MessageUserPositionInterface {
userId: string; userId: string;
roomId: string;
name: string; name: string;
character: string; character: string;
position: PointInterface; position: PointInterface;
@ -86,20 +67,10 @@ export interface MessageUserPositionInterface {
class MessageUserPosition extends Message implements MessageUserPositionInterface{ class MessageUserPosition extends Message implements MessageUserPositionInterface{
position: PointInterface; position: PointInterface;
constructor(userId : string, roomId : string, point : Point, name: string, character: string) { constructor(userId : string, point : Point, name: string, character: string) {
super(userId, roomId, name, character); super(userId, name, character);
this.position = point; this.position = point;
} }
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
}
} }
export interface ListMessageUserPositionInterface { export interface ListMessageUserPositionInterface {
@ -117,7 +88,6 @@ class ListMessageUserPosition {
data.forEach((userPosition: any) => { data.forEach((userPosition: any) => {
this.listUsersPosition.push(new MessageUserPosition( this.listUsersPosition.push(new MessageUserPosition(
userPosition.userId, userPosition.userId,
userPosition.roomId,
new Point( new Point(
userPosition.position.x, userPosition.position.x,
userPosition.position.y, userPosition.position.y,
@ -143,10 +113,10 @@ export interface GroupCreatedUpdatedMessageInterface {
export interface ConnexionInterface { export interface ConnexionInterface {
socket: any; socket: any;
token: string; token: string;
email: string; name: string;
userId: string; userId: string;
createConnexion(characterSelected: string): Promise<any>; createConnexion(name: string, characterSelected: string): Promise<any>;
loadMaps(): Promise<any>; loadMaps(): Promise<any>;
@ -167,24 +137,24 @@ export interface ConnexionInterface {
} }
export class Connexion implements ConnexionInterface { export class Connexion implements ConnexionInterface {
socket: any; socket: Socket;
token: string; token: string;
email: string; name: string; // TODO: drop "name" storage here
character: string;
userId: string; userId: string;
GameManager: GameManager; GameManager: GameManager;
lastPositionShared: MessageUserPosition = null; lastPositionShared: PointInterface = null;
lastRoom: string|null = null;
constructor(email : string, GameManager: GameManager) { constructor(GameManager: GameManager) {
this.email = email;
this.GameManager = GameManager; this.GameManager = GameManager;
} }
/** createConnexion(name: string, characterSelected: string): Promise<ConnexionInterface> {
* @param characterSelected this.name = name;
*/ this.character = characterSelected;
createConnexion(characterSelected: string): Promise<ConnexionInterface> {
/*return Axios.post(`${API_URL}/login`, {email: this.email}) /*return Axios.post(`${API_URL}/login`, {email: this.email})
.then((res) => { .then((res) => {
this.token = res.data.token; this.token = res.data.token;
@ -196,19 +166,7 @@ export class Connexion implements ConnexionInterface {
}*/ }*/
}); });
this.connectSocketServer(); return this.connectSocketServer();
// TODO: maybe trigger promise only when connexion is established?
let promise = new Promise<ConnexionInterface>((resolve, reject) => {
/*console.log('PROMISE CREATED')
this.socket.on('connection', () => {
console.log('CONNECTED');
resolve(this);
});*/
resolve(this);
});
return promise;
/* return res.data; /* return res.data;
}) })
@ -222,32 +180,42 @@ export class Connexion implements ConnexionInterface {
* *
* @param character * @param character
*/ */
connectSocketServer(character : string = null){ connectSocketServer(): Promise<ConnexionInterface>{
//if try to reconnect with last position
if(this.lastPositionShared) {
//join the room
this.joinARoom(
this.lastPositionShared.roomId,
this.lastPositionShared.character
);
//share your first position
this.sharePosition(
this.lastPositionShared ? this.lastPositionShared.position.x : 0,
this.lastPositionShared ? this.lastPositionShared.position.y : 0,
this.lastPositionShared.character,
this.lastPositionShared.roomId,
this.lastPositionShared.position.direction
);
}
//listen event //listen event
this.attributeUserId();
this.positionOfAllUser(); this.positionOfAllUser();
this.disconnectServer(); this.disconnectServer();
this.errorMessage(); this.errorMessage();
this.groupUpdatedOrCreated(); this.groupUpdatedOrCreated();
this.groupDeleted(); this.groupDeleted();
return new Promise<ConnexionInterface>((resolve, reject) => {
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, {
name: this.name,
character: this.character
} as SetPlayerDetailsMessage, (id: string) => {
this.userId = id;
});
//if try to reconnect with last position
if(this.lastRoom) {
//join the room
this.joinARoom(
this.lastRoom
);
}
if(this.lastPositionShared) {
//share your first position
this.sharePosition(
this.lastPositionShared ? this.lastPositionShared.x : 0,
this.lastPositionShared ? this.lastPositionShared.y : 0,
this.lastPositionShared.direction
);
}
resolve(this);
});
} }
//TODO add middleware with access token to secure api //TODO add middleware with access token to secure api
@ -266,15 +234,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.email,
character
);
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition.toString());
} }
/** /**
@ -285,28 +247,13 @@ export class Connexion implements ConnexionInterface {
* @param roomId * @param roomId
* @param direction * @param direction
*/ */
sharePosition(x : number, y : number, character : string, roomId : string, direction : string = "none") : void{ sharePosition(x : number, y : number, direction : string = "none") : void{
if(!this.socket){ if(!this.socket){
return; return;
} }
let messageUserPosition = new MessageUserPosition( let point = new Point(x, y, direction);
this.userId, this.lastPositionShared = point;
roomId, this.socket.emit(EventMessage.USER_POSITION, point);
new Point(x, y, direction),
this.email,
character
);
this.lastPositionShared = messageUserPosition;
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition.toString());
}
attributeUserId(): void {
// This event is received as soon as the connexion is established.
// It allows informing the browser of its own user id.
this.socket.on(EventMessage.ATTRIBUTE_USER_ID, (userId: string) => {
console.log('Received my user id: ', userId);
this.userId = userId;
});
} }
/** /**
@ -326,7 +273,7 @@ export class Connexion implements ConnexionInterface {
**/ **/
positionOfAllUser(): void { positionOfAllUser(): void {
this.socket.on(EventMessage.USER_POSITION, (message: string) => { this.socket.on(EventMessage.USER_POSITION, (message: string) => {
let dataList = JSON.parse(message); let dataList = message;
let UserPositions : Array<any> = Object.values(dataList); let UserPositions : Array<any> = Object.values(dataList);
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]); let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
this.GameManager.shareUserPosition(listMessageUserPosition); this.GameManager.shareUserPosition(listMessageUserPosition);
@ -347,12 +294,12 @@ export class Connexion implements ConnexionInterface {
} }
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) { sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, JSON.stringify({ return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
userId: userId ? userId : this.userId, userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId, receiverId: receiverId ? receiverId : this.userId,
roomId: roomId, roomId: roomId,
signal: signal signal: signal
})); });
} }
receiveWebrtcStart(callback: Function) { receiveWebrtcStart(callback: Function) {

View File

@ -0,0 +1,4 @@
export interface SetPlayerDetailsMessage {
name: string,
character: string
}

View File

@ -17,7 +17,6 @@ export interface HasMovedEvent {
direction: string; direction: string;
x: number; x: number;
y: number; y: number;
character: string;
} }
export interface MapObject { export interface MapObject {
@ -40,8 +39,8 @@ export class GameManager {
connect(name: string, characterUserSelected : string) { connect(name: string, characterUserSelected : string) {
this.playerName = name; this.playerName = name;
this.characterUserSelected = characterUserSelected; this.characterUserSelected = characterUserSelected;
this.ConnexionInstance = new Connexion(name, this); this.ConnexionInstance = new Connexion(this);
return this.ConnexionInstance.createConnexion(characterUserSelected).then((data : any) => { return this.ConnexionInstance.createConnexion(name, characterUserSelected).then((data : any) => {
this.SimplePeer = new SimplePeer(this.ConnexionInstance); this.SimplePeer = new SimplePeer(this.ConnexionInstance);
return data; return data;
}).catch((err) => { }).catch((err) => {
@ -71,8 +70,8 @@ export class GameManager {
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED; this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
} }
joinRoom(sceneKey : string, character: string){ joinRoom(sceneKey : string){
this.ConnexionInstance.joinARoom(sceneKey, character); this.ConnexionInstance.joinARoom(sceneKey);
} }
/** /**
@ -128,7 +127,7 @@ export class GameManager {
} }
pushPlayerPosition(event: HasMovedEvent) { pushPlayerPosition(event: HasMovedEvent) {
this.ConnexionInstance.sharePosition(event.x, event.y, event.character, this.currentGameScene.scene.key, event.direction); this.ConnexionInstance.sharePosition(event.x, event.y, event.direction);
} }
loadMap(mapUrl: string, scene: ScenePlugin): string { loadMap(mapUrl: string, scene: ScenePlugin): string {

View File

@ -284,7 +284,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
this.createCollisionObject(); this.createCollisionObject();
//join room //join room
this.GameManager.joinRoom(this.scene.key, this.CurrentPlayer.PlayerTexture); this.GameManager.joinRoom(this.scene.key);
//listen event to share position of user //listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this)) this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))

View File

@ -81,12 +81,12 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
} }
if (x !== 0 || y !== 0) { if (x !== 0 || y !== 0) {
this.move(x, y); this.move(x, y);
this.emit(hasMovedEventName, {direction, x: this.x, y: this.y, character: this.PlayerTexture}); this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
} else { } else {
if (this.previousMove !== PlayerAnimationNames.None) { if (this.previousMove !== PlayerAnimationNames.None) {
direction = PlayerAnimationNames.None; direction = PlayerAnimationNames.None;
this.stop(); this.stop();
this.emit(hasMovedEventName, {direction, x: this.x, y: this.y, character: this.PlayerTexture}); this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
} }
} }

View File

@ -32,7 +32,7 @@ export class SimplePeer implements SimplePeerInterface{
private initialise() { private initialise() {
//receive signal by gemer //receive signal by gemer
this.Connexion.receiveWebrtcSignal((message: string) => { this.Connexion.receiveWebrtcSignal((message: any) => {
this.receiveWebrtcSignal(message); this.receiveWebrtcSignal(message);
}); });
@ -40,7 +40,7 @@ export class SimplePeer implements SimplePeerInterface{
this.MediaManager.getCamera().then(() => { this.MediaManager.getCamera().then(() => {
//receive message start //receive message start
this.Connexion.receiveWebrtcStart((message: string) => { this.Connexion.receiveWebrtcStart((message: any) => {
this.receiveWebrtcStart(message); this.receiveWebrtcStart(message);
}); });
@ -49,18 +49,12 @@ export class SimplePeer implements SimplePeerInterface{
}); });
//receive signal by gemer //receive signal by gemer
this.Connexion.disconnectMessage((message: string) => { this.Connexion.disconnectMessage((data: any) => {
let data = JSON.parse(message);
this.closeConnexion(data.userId); this.closeConnexion(data.userId);
}); });
} }
/** private receiveWebrtcStart(data: any) {
*
* @param message
*/
private receiveWebrtcStart(message: string) {
let data = JSON.parse(message);
this.WebRtcRoomId = data.roomId; this.WebRtcRoomId = data.roomId;
this.Users = data.clients; this.Users = data.clients;
@ -193,12 +187,7 @@ export class SimplePeer implements SimplePeerInterface{
} }
} }
/** private receiveWebrtcSignal(data: any) {
*
* @param message
*/
private receiveWebrtcSignal(message: string) {
let data = JSON.parse(message);
try { try {
//if offer type, create peer connexion //if offer type, create peer connexion
if(data.signal.type === "offer"){ if(data.signal.type === "offer"){