Adding a "silent" notion (triggered in Jitsi meets)

This commit is contained in:
David Négrier 2020-08-31 14:03:40 +02:00
parent 0a8ba37049
commit df7b5cc2e3
5 changed files with 68 additions and 7 deletions

View File

@ -33,7 +33,8 @@ 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" SET_PLAYER_DETAILS = "set-player-details",
SET_SILENT = "set_silent", // Set or unset the silent mode for this user.
} }
export class IoSocketController { export class IoSocketController {
@ -274,6 +275,29 @@ export class IoSocketController {
Client.characterLayers = playerDetails.characterLayers; Client.characterLayers = playerDetails.characterLayers;
answerFn(Client.userId); answerFn(Client.userId);
}); });
socket.on(SockerIoEvent.SET_SILENT, (silent: unknown) => {
if (typeof silent !== "boolean") {
socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: 'Invalid SET_SILENT message.'});
console.warn('Invalid SET_SILENT message received: ', silent);
return;
}
try {
const Client = (socket as ExSocketInterface);
// update position in the world
const world = this.Worlds.get(Client.roomId);
if (!world) {
console.error("Could not find world with id '", Client.roomId, "'");
return;
}
world.setSilent(Client, silent);
} catch (e) {
console.error('An error occurred on "SET_SILENT"');
console.error(e);
}
});
}); });
} }

View File

@ -4,5 +4,6 @@ import { PointInterface } from "./Websocket/PointInterface";
export interface UserInterface { export interface UserInterface {
id: string, id: string,
group?: Group, group?: Group,
position: PointInterface position: PointInterface,
silent: boolean
} }

View File

@ -55,7 +55,8 @@ export class World {
public join(socket : Identificable, userPosition: PointInterface): void { public join(socket : Identificable, userPosition: PointInterface): void {
this.users.set(socket.userId, { this.users.set(socket.userId, {
id: socket.userId, id: socket.userId,
position: userPosition position: userPosition,
silent: false // FIXME: silent should be set at the correct value when joining a room.
}); });
// 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);
@ -84,6 +85,10 @@ export class World {
user.position = userPosition; user.position = userPosition;
if (user.silent) {
return;
}
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:
// should he join a group? // should he join a group?
@ -118,6 +123,26 @@ export class World {
} }
} }
setSilent(socket: Identificable, silent: boolean) {
const user = this.users.get(socket.userId);
if(typeof user === 'undefined') {
console.warn('In setSilent, could not find user with ID "'+socket.userId+'" in world.');
return;
}
if (user.silent === silent) {
return;
}
user.silent = silent;
if (silent && user.group !== undefined) {
this.leaveGroup(user);
}
if (!silent) {
// If we are back to life, let's trigger a position update to see if we can join some group.
this.updatePosition(socket, user.position);
}
}
/** /**
* Makes a user leave a group and closes and destroy the group if the group contains only one remaining person. * Makes a user leave a group and closes and destroy the group if the group contains only one remaining person.
* *
@ -145,6 +170,7 @@ export class World {
* Looks for the closest user that is: * Looks for the closest user that is:
* - close enough (distance <= minDistance) * - close enough (distance <= minDistance)
* - not in a group * - not in a group
* - not silent
* OR * OR
* - close enough to a group (distance <= groupRadius) * - close enough to a group (distance <= groupRadius)
*/ */
@ -160,6 +186,9 @@ export class World {
if(currentUser === user) { if(currentUser === user) {
return; return;
} }
if (currentUser.silent) {
return;
}
const distance = World.computeDistance(user, currentUser); // compute distance between peers. const distance = World.computeDistance(user, currentUser); // compute distance between peers.

View File

@ -24,6 +24,7 @@ enum EventMessage{
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id. SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
CONNECT_ERROR = "connect_error", CONNECT_ERROR = "connect_error",
SET_SILENT = "set_silent", // Set or unset the silent mode for this user.
} }
export interface PointInterface { export interface PointInterface {
@ -167,6 +168,10 @@ export class Connection implements Connection {
this.socket.emit(EventMessage.USER_POSITION, point); this.socket.emit(EventMessage.USER_POSITION, point);
} }
public setSilent(silent: boolean): void {
this.socket.emit(EventMessage.SET_SILENT, silent);
}
public onUserJoins(callback: (message: MessageUserJoined) => void): void { public onUserJoins(callback: (message: MessageUserJoined) => void): void {
this.socket.on(EventMessage.JOIN_ROOM, callback); this.socket.on(EventMessage.JOIN_ROOM, callback);
} }

View File

@ -423,9 +423,10 @@ export class GameScene extends Phaser.Scene implements CenterListener {
CoWebsiteManager.loadCoWebsite(newValue as string); CoWebsiteManager.loadCoWebsite(newValue as string);
} }
}); });
let jitsiApi: any; let jitsiApi: any; // eslint-disable-line @typescript-eslint/no-explicit-any
this.gameMap.onPropertyChange('jitsiRoom', (newValue, oldValue) => { this.gameMap.onPropertyChange('jitsiRoom', (newValue, oldValue) => {
if (newValue === undefined) { if (newValue === undefined) {
this.connection.setSilent(false);
jitsiApi?.dispose(); jitsiApi?.dispose();
CoWebsiteManager.closeCoWebsite(); CoWebsiteManager.closeCoWebsite();
} else { } else {
@ -444,9 +445,10 @@ export class GameScene extends Phaser.Scene implements CenterListener {
MOBILE_APP_PROMO: false MOBILE_APP_PROMO: false
} }
}; };
jitsiApi = new (window as any).JitsiMeetExternalAPI(domain, options); jitsiApi = new (window as any).JitsiMeetExternalAPI(domain, options); // eslint-disable-line @typescript-eslint/no-explicit-any
jitsiApi.executeCommand('displayName', gameManager.getPlayerName()); jitsiApi.executeCommand('displayName', gameManager.getPlayerName());
})) }));
this.connection.setSilent(true);
} }
}); });
} }