Use userId
generated by back end in all message
This commit is contained in:
parent
ab32021fc0
commit
0c9cbca765
@ -51,7 +51,7 @@ export class IoSocketController {
|
|||||||
return next(new Error('Authentication error'));
|
return next(new Error('Authentication error'));
|
||||||
}
|
}
|
||||||
(socket as ExSocketInterface).token = tokenDecoded;
|
(socket as ExSocketInterface).token = tokenDecoded;
|
||||||
(socket as ExSocketInterface).id = tokenDecoded.userId;
|
(socket as ExSocketInterface).userId = tokenDecoded.userId;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -97,7 +97,8 @@ export class IoSocketController {
|
|||||||
|
|
||||||
ioConnection() {
|
ioConnection() {
|
||||||
this.Io.on(SockerIoEvent.CONNECTION, (socket: Socket) => {
|
this.Io.on(SockerIoEvent.CONNECTION, (socket: Socket) => {
|
||||||
this.sockets.set(socket.id, socket as ExSocketInterface);
|
let client : ExSocketInterface = socket as ExSocketInterface;
|
||||||
|
this.sockets.set(client.userId, client);
|
||||||
/*join-rom event permit to join one room.
|
/*join-rom event permit to join one room.
|
||||||
message :
|
message :
|
||||||
userId : user identification
|
userId : user identification
|
||||||
@ -135,7 +136,7 @@ export class IoSocketController {
|
|||||||
//add function to refresh position user in real time.
|
//add function to refresh position user in real time.
|
||||||
//this.refreshUserPosition(Client);
|
//this.refreshUserPosition(Client);
|
||||||
|
|
||||||
let messageUserJoined = new MessageUserJoined(Client.id, Client.name, Client.character, Client.position);
|
let messageUserJoined = new MessageUserJoined(Client.userId, Client.name, Client.character, Client.position);
|
||||||
|
|
||||||
socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserJoined);
|
socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserJoined);
|
||||||
|
|
||||||
@ -172,7 +173,7 @@ export class IoSocketController {
|
|||||||
}
|
}
|
||||||
world.updatePosition(Client, position);
|
world.updatePosition(Client, position);
|
||||||
|
|
||||||
socket.to(Client.roomId).emit(SockerIoEvent.USER_MOVED, new MessageUserMoved(Client.id, Client.position));
|
socket.to(Client.roomId).emit(SockerIoEvent.USER_MOVED, new MessageUserMoved(Client.userId, Client.position));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('An error occurred on "user_position" event');
|
console.error('An error occurred on "user_position" event');
|
||||||
console.error(e);
|
console.error(e);
|
||||||
@ -200,9 +201,8 @@ export class IoSocketController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on(SockerIoEvent.DISCONNECT, () => {
|
socket.on(SockerIoEvent.DISCONNECT, () => {
|
||||||
|
let Client = (socket as ExSocketInterface);
|
||||||
try {
|
try {
|
||||||
let Client = (socket as ExSocketInterface);
|
|
||||||
|
|
||||||
//leave room
|
//leave room
|
||||||
this.leaveRoom(Client);
|
this.leaveRoom(Client);
|
||||||
|
|
||||||
@ -218,7 +218,7 @@ export class IoSocketController {
|
|||||||
console.error('An error occurred on "disconnect"');
|
console.error('An error occurred on "disconnect"');
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
this.sockets.delete(socket.id);
|
this.sockets.delete(Client.userId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Let's send the user id to the user
|
// Let's send the user id to the user
|
||||||
@ -226,7 +226,7 @@ export class IoSocketController {
|
|||||||
let Client = (socket as ExSocketInterface);
|
let Client = (socket as ExSocketInterface);
|
||||||
Client.name = playerDetails.name;
|
Client.name = playerDetails.name;
|
||||||
Client.character = playerDetails.character;
|
Client.character = playerDetails.character;
|
||||||
answerFn(socket.id);
|
answerFn(Client.userId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -242,7 +242,7 @@ export class IoSocketController {
|
|||||||
leaveRoom(Client : ExSocketInterface){
|
leaveRoom(Client : ExSocketInterface){
|
||||||
// leave previous room and world
|
// leave previous room and world
|
||||||
if(Client.roomId){
|
if(Client.roomId){
|
||||||
Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.id);
|
Client.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, Client.userId);
|
||||||
|
|
||||||
//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);
|
||||||
@ -310,11 +310,11 @@ export class IoSocketController {
|
|||||||
clients.forEach((client: ExSocketInterface, index: number) => {
|
clients.forEach((client: ExSocketInterface, index: number) => {
|
||||||
|
|
||||||
let clientsId = clients.reduce((tabs: Array<any>, clientId: ExSocketInterface, indexClientId: number) => {
|
let clientsId = clients.reduce((tabs: Array<any>, clientId: ExSocketInterface, indexClientId: number) => {
|
||||||
if (!clientId.id || clientId.id === client.id) {
|
if (!clientId.userId || clientId.userId === client.userId) {
|
||||||
return tabs;
|
return tabs;
|
||||||
}
|
}
|
||||||
tabs.push({
|
tabs.push({
|
||||||
userId: clientId.id,
|
userId: clientId.userId,
|
||||||
name: clientId.name,
|
name: clientId.name,
|
||||||
initiator: index <= indexClientId
|
initiator: index <= indexClientId
|
||||||
});
|
});
|
||||||
|
@ -6,7 +6,7 @@ export interface ExSocketInterface extends Socket, Identificable {
|
|||||||
token: any;
|
token: any;
|
||||||
roomId: string;
|
roomId: string;
|
||||||
webRtcRoomId: string;
|
webRtcRoomId: string;
|
||||||
//userId: string;
|
userId: string;
|
||||||
name: string;
|
name: string;
|
||||||
character: string;
|
character: string;
|
||||||
position: PointInterface;
|
position: PointInterface;
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
export interface Identificable {
|
export interface Identificable {
|
||||||
id: string;
|
userId: string;
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,8 @@ export class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public join(socket : Identificable, userPosition: PointInterface): void {
|
public join(socket : Identificable, userPosition: PointInterface): void {
|
||||||
this.users.set(socket.id, {
|
this.users.set(socket.userId, {
|
||||||
id: socket.id,
|
id: socket.userId,
|
||||||
position: userPosition
|
position: userPosition
|
||||||
});
|
});
|
||||||
// Let's call update position to trigger the join / leave room
|
// Let's call update position to trigger the join / leave room
|
||||||
@ -62,18 +62,18 @@ export class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public leave(user : Identificable){
|
public leave(user : Identificable){
|
||||||
let userObj = this.users.get(user.id);
|
let userObj = this.users.get(user.userId);
|
||||||
if (userObj === undefined) {
|
if (userObj === undefined) {
|
||||||
console.warn('User ', user.id, 'does not belong to world! It should!');
|
console.warn('User ', user.userId, 'does not belong to world! It should!');
|
||||||
}
|
}
|
||||||
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
|
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
|
||||||
this.leaveGroup(userObj);
|
this.leaveGroup(userObj);
|
||||||
}
|
}
|
||||||
this.users.delete(user.id);
|
this.users.delete(user.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public updatePosition(socket : Identificable, userPosition: PointInterface): void {
|
public updatePosition(socket : Identificable, userPosition: PointInterface): void {
|
||||||
let user = this.users.get(socket.id);
|
let user = this.users.get(socket.userId);
|
||||||
if(typeof user === 'undefined') {
|
if(typeof user === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user