Sending player details (name + character selected) on connection

This commit is contained in:
David Négrier 2020-05-15 22:40:06 +02:00
parent fff0e13a2e
commit b80e3e07d8
5 changed files with 48 additions and 45 deletions

View File

@ -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 {
@ -125,7 +126,7 @@ export class IoSocketController {
} }
}); });
socket.on(SockerIoEvent.USER_POSITION, (message: string) => { socket.on(SockerIoEvent.USER_POSITION, (message: any) => {
try { try {
let messageUserPosition = this.hydrateMessageReceive(message); let messageUserPosition = this.hydrateMessageReceive(message);
if (messageUserPosition instanceof Error) { if (messageUserPosition instanceof Error) {
@ -192,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);
});
}); });
} }

View File

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

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,7 +21,7 @@ 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 {
@ -115,10 +117,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>;
@ -139,24 +141,23 @@ 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: MessageUserPosition = 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;
@ -168,19 +169,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;
}) })
@ -194,7 +183,7 @@ export class Connexion implements ConnexionInterface {
* *
* @param character * @param character
*/ */
connectSocketServer(character : string = null){ connectSocketServer(): Promise<ConnexionInterface>{
//if try to reconnect with last position //if try to reconnect with last position
if(this.lastPositionShared) { if(this.lastPositionShared) {
//join the room //join the room
@ -214,12 +203,21 @@ export class Connexion implements ConnexionInterface {
} }
//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;
});
resolve(this);
});
} }
//TODO add middleware with access token to secure api //TODO add middleware with access token to secure api
@ -243,7 +241,7 @@ export class Connexion implements ConnexionInterface {
this.userId, this.userId,
roomId, roomId,
new Point(0, 0), new Point(0, 0),
this.email, this.name,
character character
); );
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition); this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
@ -265,22 +263,13 @@ export class Connexion implements ConnexionInterface {
this.userId, this.userId,
roomId, roomId,
new Point(x, y, direction), new Point(x, y, direction),
this.email, this.name,
character character
); );
this.lastPositionShared = messageUserPosition; this.lastPositionShared = messageUserPosition;
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition); this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
} }
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 : * The data sent is an array with information for each user :
* [ * [

View File

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

View File

@ -40,8 +40,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) => {