Connect and Disconnect event.

This commit is contained in:
gparant 2020-05-03 16:28:18 +02:00
parent 8357f9b8c2
commit 372f938bbb

View File

@ -25,6 +25,7 @@ enum SockerIoEvent {
export class IoSocketController { export class IoSocketController {
Io: socketIO.Server; Io: socketIO.Server;
World: World; World: World;
constructor(server: http.Server) { constructor(server: http.Server) {
this.Io = socketIO(server); this.Io = socketIO(server);
@ -125,9 +126,7 @@ export class IoSocketController{
socket.on(SockerIoEvent.DISCONNECT, () => { socket.on(SockerIoEvent.DISCONNECT, () => {
let Client = (socket as ExSocketInterface); let Client = (socket as ExSocketInterface);
socket.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, JSON.stringify({ this.sendDisconnectedEvent(Client);
userId: Client.userId
}));
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
this.refreshUserPosition(); this.refreshUserPosition();
@ -164,6 +163,17 @@ export class IoSocketController{
} }
return null; return null;
} }
/**
*
* @param Client: ExSocketInterface
*/
sendDisconnectedEvent(Client: ExSocketInterface) {
Client.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, JSON.stringify({
userId: Client.userId
}));
}
/** /**
* *
* @param socket * @param socket
@ -243,6 +253,7 @@ export class IoSocketController{
] ]
**/ **/
seTimeOutInProgress: any = null; seTimeOutInProgress: any = null;
shareUsersPosition() { shareUsersPosition() {
if (this.seTimeOutInProgress) { if (this.seTimeOutInProgress) {
clearTimeout(this.seTimeOutInProgress); clearTimeout(this.seTimeOutInProgress);
@ -265,27 +276,20 @@ export class IoSocketController{
} }
//connected user //connected user
connectedUser(user1 : string, user2 : string, group : Group) { connectedUser(userId: string, group: Group) {
if(!group){ let Client = this.searchClientById(userId);
if (!Client) {
return; return;
} }
/* TODO manager room and group user to enter and leave */ this.joinWebRtcRoom(Client, group.getId());
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
let User1 = clients.find((user: ExSocketInterface) => user.userId === user1);
let User2 = clients.find((user: ExSocketInterface) => user.userId === user2);
if (User1) {
this.joinWebRtcRoom(User1, group.getId());
}
if (User2) {
this.joinWebRtcRoom(User2, group.getId());
}
} }
//connected user //connected user
disConnectedUser(user1 : string, user2 : string, group : Group){ disConnectedUser(userId: string, group: Group) {
console.log("disConnectedUser => user1", user1); let Client = this.searchClientById(userId);
console.log("disConnectedUser => user2", user2); if (!Client) {
console.log("group", group); return;
}
this.sendDisconnectedEvent(Client)
} }
} }