Previously, userid was generated by the "/login" route and passed along.
This commit completely removes the uuid "userid" (and disables the LoginController too and any Jwt check).
"userid" is replaced by the "socket id" of the connection.
So a user is now identified using a socket id, which is unique for a given connection.
import {SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import { uuid } from 'uuidv4';
-export class AuthenticateController{
+export class AuthenticateController {
App : Application;
constructor(App : Application) {
//permit to login on application. Return token to connect on Websocket IO.
login(){
- this.App.post("/login", (req: Request, res: Response) => {
+ // For now, let's completely forget the /login route.
+ /*this.App.post("/login", (req: Request, res: Response) => {
let param = req.body;
if(!param.email){
return res.status(BAD_REQUEST).send({
mapUrlStart: URL_ROOM_STARTED,
userId: userId,
});
- });
+ });*/
}
-}
\ No newline at end of file
+}
enum SockerIoEvent {
CONNECTION = "connection",
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",
USER_POSITION = "user-position",
WEBRTC_SIGNAL = "webrtc-signal",
this.Io = socketIO(server);
// Authentication with token. it will be decoded and stored in the socket.
- this.Io.use((socket: Socket, next) => {
+ // Completely commented for now, as we do not use the "/login" route at all.
+ /*this.Io.use((socket: Socket, next) => {
if (!socket.handshake.query || !socket.handshake.query.token) {
return next(new Error('Authentication error'));
}
(socket as ExSocketInterface).token = tokenDecoded;
next();
});
- });
+ });*/
this.ioConnection();
this.shareUsersPosition();
let userId = lastUser.id;
let client: ExSocketInterface|null = this.searchClientById(userId);
if (client === null) {
+ console.warn('Could not find client ', userId, ' in group')
return;
}
let roomId = client.roomId;
socket.leave(Client.webRtcRoomId);
//delete all socket information
- delete Client.userId;
delete Client.webRtcRoomId;
delete Client.roomId;
delete Client.token;
console.error(e);
}
});
+
+ // Let's send the user id to the user
+ socket.emit(SockerIoEvent.ATTRIBUTE_USER_ID, socket.id);
});
}
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
for (let i = 0; i < clients.length; i++) {
let client: ExSocketInterface = clients[i];
- if (client.userId !== userId) {
- continue
+ if (client.id !== userId) {
+ continue;
}
return client;
}
+ console.log("Could not find user with id ", userId);
return null;
}
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
for (let i = 0; i < clients.length; i++) {
let client: ExSocketInterface = clients[i];
- if (client.userId !== userId) {
+ if (client.id !== userId) {
continue
}
return client;
*/
sendDisconnectedEvent(Client: ExSocketInterface) {
Client.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, JSON.stringify({
- userId: Client.userId
+ userId: Client.id
}));
//disconnect webrtc room
leaveRoom(Client : ExSocketInterface){
//lease previous room and world
if(Client.roomId){
- //user leave previous room
- Client.leave(Client.roomId);
//user leave previous world
let world : World|undefined = this.Worlds.get(Client.roomId);
if(world){
+ console.log('Entering world.leave')
world.leave(Client);
- this.Worlds.set(Client.roomId, world);
+ //this.Worlds.set(Client.roomId, world);
}
+ //user leave previous room
+ Client.leave(Client.roomId);
+ delete Client.roomId;
}
}
/**
});
});
//join world
- world.join(messageUserPosition);
+ world.join(Client, messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}
clients.forEach((client: ExSocketInterface, index: number) => {
let clientsId = clients.reduce((tabs: Array<any>, clientId: ExSocketInterface, indexClientId: number) => {
- if (!clientId.userId || clientId.userId === client.userId) {
+ if (!clientId.id || clientId.id === client.id) {
return tabs;
}
tabs.push({
- userId: clientId.userId,
+ userId: clientId.id,
name: clientId.name,
initiator: index <= indexClientId
});
saveUserInformation(socket: ExSocketInterface, message: MessageUserPosition) {
socket.position = message.position;
socket.roomId = message.roomId;
- socket.userId = message.userId;
+ //socket.userId = message.userId;
socket.name = message.name;
socket.character = message.character;
}
}
rooms.refreshUserPosition(rooms, this.Io);
- // update position in the worl
+ // update position in the world
let data = {
- userId: Client.userId,
+ userId: Client.id,
roomId: Client.roomId,
position: Client.position,
name: Client.name,
if (!world) {
return;
}
- world.updatePosition(messageUserPosition);
+ world.updatePosition(Client, messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}
import {Socket} from "socket.io";
import {PointInterface} from "./PointInterface";
+import {Identificable} from "./Identificable";
-export interface ExSocketInterface extends Socket {
+export interface ExSocketInterface extends Socket, Identificable {
token: any;
roomId: string;
webRtcRoomId: string;
- userId: string;
+ //userId: string;
name: string;
character: string;
position: PointInterface;
continue;
}
let data = {
- userId: socket.userId,
+ userId: socket.id,
roomId: socket.roomId,
position: socket.position,
name: socket.name,
--- /dev/null
+export interface Identificable {
+ id: string;
+}
}
toJson() {
+
return {
userId: this.userId,
roomId: this.roomId,
import {UserInterface} from "./UserInterface";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface";
+import {Identificable} from "_Model/Websocket/Identificable";
export type ConnectCallback = (user: string, group: Group) => void;
export type DisconnectCallback = (user: string, group: Group) => void;
export type GroupDeletedCallback = (uuid: string, lastUser: UserInterface) => void;
export class World {
- private minDistance: number;
- private groupRadius: number;
+ private readonly minDistance: number;
+ private readonly groupRadius: number;
// Users, sorted by ID
- private users: Map<string, UserInterface>;
- private groups: Group[];
+ private readonly users: Map<string, UserInterface>;
+ private readonly groups: Group[];
- private connectCallback: ConnectCallback;
- private disconnectCallback: DisconnectCallback;
- private groupUpdatedCallback: GroupUpdatedCallback;
- private groupDeletedCallback: GroupDeletedCallback;
+ private readonly connectCallback: ConnectCallback;
+ private readonly disconnectCallback: DisconnectCallback;
+ private readonly groupUpdatedCallback: GroupUpdatedCallback;
+ private readonly groupDeletedCallback: GroupDeletedCallback;
constructor(connectCallback: ConnectCallback,
disconnectCallback: DisconnectCallback,
return this.groups;
}
- public join(userPosition: MessageUserPosition): void {
- this.users.set(userPosition.userId, {
- id: userPosition.userId,
+ public join(socket : Identificable, userPosition: MessageUserPosition): void {
+ this.users.set(socket.id, {
+ id: socket.id,
position: userPosition.position
});
// Let's call update position to trigger the join / leave room
- this.updatePosition(userPosition);
+ this.updatePosition(socket, userPosition);
}
- public leave(user : ExSocketInterface){
+ public leave(user : Identificable){
let userObj = this.users.get(user.id);
+ if (userObj === undefined) {
+ // FIXME: this seems always wrong. I guess user.id is different from userPosition.userId
+ console.warn('User ', user.id, 'does not belong to world! It should!');
+ }
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
- this.leaveGroup(user);
+ this.leaveGroup(userObj);
}
- this.users.delete(user.userId);
+ this.users.delete(user.id);
}
- public updatePosition(userPosition: MessageUserPosition): void {
- let user = this.users.get(userPosition.userId);
+ public updatePosition(socket : Identificable, userPosition: MessageUserPosition): void {
+ let user = this.users.get(socket.id);
if(typeof user === 'undefined') {
return;
}
throw new Error("The user is part of no group");
}
group.leave(user);
-
if (group.isEmpty()) {
this.groupDeletedCallback(group.getId(), user);
group.destroy();
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
- world.join(new MessageUserPosition({
- userId: "foo",
+ world.join({ id: "foo" }, new MessageUserPosition({
+ userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
- world.join(new MessageUserPosition({
- userId: "bar",
+ world.join({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(500, 100)
}));
- world.updatePosition(new MessageUserPosition({
- userId: "bar",
+ world.updatePosition({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(261, 100)
}));
expect(connectCalledNumber).toBe(0);
- world.updatePosition(new MessageUserPosition({
- userId: "bar",
+ world.updatePosition({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(101, 100)
}));
expect(connectCalledNumber).toBe(2);
- world.updatePosition(new MessageUserPosition({
- userId: "bar",
+ world.updatePosition({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(102, 100)
}));
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
- world.join(new MessageUserPosition({
- userId: "foo",
+ world.join({ id: "foo" }, new MessageUserPosition({
+ userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
- world.join(new MessageUserPosition({
- userId: "bar",
+ world.join({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(200, 100)
}));
connectCalled = false;
// baz joins at the outer limit of the group
- world.join(new MessageUserPosition({
- userId: "baz",
+ world.join({ id: "baz" }, new MessageUserPosition({
+ userId: "bazbaz",
roomId: 1,
position: new Point(311, 100)
}));
expect(connectCalled).toBe(false);
- world.updatePosition(new MessageUserPosition({
- userId: "baz",
+ world.updatePosition({ id: "baz" }, new MessageUserPosition({
+ userId: "bazbaz",
roomId: 1,
position: new Point(309, 100)
}));
let world = new World(connect, disconnect, 160, 160, () => {}, () => {});
- world.join(new MessageUserPosition({
- userId: "foo",
+ world.join({ id: "foo" }, new MessageUserPosition({
+ userId: "foofoo",
roomId: 1,
position: new Point(100, 100)
}));
- world.join(new MessageUserPosition({
- userId: "bar",
+ world.join({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(259, 100)
}));
expect(connectCalled).toBe(true);
expect(disconnectCallNumber).toBe(0);
- world.updatePosition(new MessageUserPosition({
- userId: "bar",
+ world.updatePosition({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(100+160+160+1, 100)
}));
expect(disconnectCallNumber).toBe(2);
- world.updatePosition(new MessageUserPosition({
- userId: "bar",
+ world.updatePosition({ id: "bar" }, new MessageUserPosition({
+ userId: "barbar",
roomId: 1,
position: new Point(262, 100)
}));
GROUP_DELETE = "group-delete",
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.
}
class Message {
* @param 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) => {
this.token = res.data.token;
- this.userId = res.data.userId;
+ this.userId = res.data.userId;*/
this.socket = SocketIo(`${API_URL}`, {
- query: {
+ /*query: {
token: this.token
- }
+ }*/
});
this.connectSocketServer();
- return res.data;
+ // 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;
})
.catch((err) => {
console.error(err);
throw err;
- });
+ });*/
}
/**
}
//listen event
+ this.attributeUserId();
this.positionOfAllUser();
this.disconnectServer();
this.errorMessage();
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;
+ });
+ }
+
/**
* The data sent is an array with information for each user :
* [
*/
createCurrentPlayer(): void {
//Get started room send by the backend
- this.currentGameScene.createCurrentPlayer(this.ConnexionInstance.userId);
+ this.currentGameScene.createCurrentPlayer();
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
}
return this.playerName;
}
+ getPlayerId(): string {
+ return this.ConnexionInstance.userId;
+ }
+
getCharacterSelected(): string {
return this.characterUserSelected;
}
export interface GameSceneInterface extends Phaser.Scene {
Map: Phaser.Tilemaps.Tilemap;
- createCurrentPlayer(UserId : string) : void;
+ createCurrentPlayer() : void;
shareUserPosition(UsersPosition : Array<MessageUserPositionInterface>): void;
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface): void;
updateOrCreateMapPlayer(UsersPosition : Array<MessageUserPositionInterface>): void;
})
}
- createCurrentPlayer(UserId : string){
+ createCurrentPlayer(){
//initialise player
//TODO create animation moving between exit and strat
this.CurrentPlayer = new Player(
- UserId,
+ null, // The current player is not has no id (because the id can change if connexion is lost and we should check that id using the GameManager.
this,
this.startX,
this.startY,
return;
}
+ let currentPlayerId = this.GameManager.getPlayerId();
+
//add or create new user
UsersPosition.forEach((userPosition : MessageUserPositionInterface) => {
- if(userPosition.userId === this.CurrentPlayer.userId){
+ if(userPosition.userId === currentPlayerId){
return;
}
let player = this.findPlayerInMap(userPosition.userId);
});
/*create user*/
- this.createCurrentPlayer("test");
+ this.createCurrentPlayer();
cypressAsserter.initFinished();
}
throw new Error("Method not implemented.");
}
- createCurrentPlayer(UserId: string): void {
+ createCurrentPlayer(): void {
for (let i = 0; i <PLAYER_RESOURCES.length; i++) {
let playerResource = PLAYER_RESOURCES[i];
let player = this.physics.add.sprite(playerResource.x, playerResource.y, playerResource.name, playerResource.name);
export const hasMovedEventName = "hasMoved";
export interface CurrentGamerInterface extends PlayableCaracter{
- userId : string;
initAnimation() : void;
moveUser(delta: number) : void;
say(text : string) : void;