cleaned event_handlers.js

This commit is contained in:
Janis
2023-01-11 13:46:27 +01:00
parent 6fdc0d4ee0
commit 601d7b8302

View File

@@ -1,30 +1,44 @@
// Handle the auth message event which is send by the client to init the session
// The clients send therefore all channels and clients to us
function handleAuthMessage(data) { function handleAuthMessage(data) {
// Set channels and clients
channelList.setItems(parseChannelInfos(data.payload.connections[0].channelInfos)); channelList.setItems(parseChannelInfos(data.payload.connections[0].channelInfos));
clientList.setItems(parseClientInfos(data.payload.connections[0].clientInfos)); clientList.setItems(parseClientInfos(data.payload.connections[0].clientInfos));
// The client of this current user
selfClient = clientList.getById(data.payload.connections[0].clientId); selfClient = clientList.getById(data.payload.connections[0].clientId);
} }
// Handle the event when a client moved to another place/channel
// Also includes disconnecting and connecting of clients
function handleClientMoved(data) { function handleClientMoved(data) {
// Get our client object based on the target client id of this event
// This can be null if the client does not exist in our list (newly joined)
const client = clientList.getById(data.payload.clientId); const client = clientList.getById(data.payload.clientId);
if (data.payload.newChannelId == 0) { if (data.payload.newChannelId == 0) {
// User disconnected // If newChannelId is 0, the client left the server
// Client disconnected
if (client) { if (client) {
console.log(`${client.name} disconnected`); console.log(`${client.name} disconnected`);
clientList.remove(client); clientList.remove(client); // Remove disconnected client from clientList
} }
// If the disconnected client is the current user
if (data.payload.clientId == thisClient.id) { if (data.payload.clientId == thisClient.id) {
//* NOTE: since this app does support multiple servers yet, we expect the user to be connected to NO servers at this point
console.log("You disconnected"); console.log("You disconnected");
clientList.clear(); clientList.clear(); // remove all clients.
} }
} else { } else {
// User moved channel // Client switched the channel OR JOINED the server to a channel
if (client) { if (client) {
// Client already exists in list // Client just switched the channel
clientList.getById(data.payload.clientId).channel = channelList.getById(data.payload.newChannelId); // Like described at the const client declaration, the client is not null therefore he already existed in our list/server
client.channel = channelList.getById(data.payload.newChannelId);
} else { } else {
console.log(data.payload); // Client joined the server
// New Client has to be created // Like described at the const client declaration, the client is null he is NEW in our list/server
clientList.add( clientList.add(
new Client( new Client(
data.payload.clientId, data.payload.clientId,
@@ -36,20 +50,29 @@ function handleClientMoved(data) {
} }
} }
// Handle the event where a client updates his properties (e.g. name, muteStatus)
function handleClientPropertiesUpdate(data) { function handleClientPropertiesUpdate(data) {
let client = clientList.getById(data.payload.clientId); // Get our client object based on the target client id of this event
// This can be null if the client does not exist in our list
const client = clientList.getById(data.payload.clientId);
if (data.payload.properties.channelGroupInheritedChannelId == 0) { if (data.payload.properties.channelGroupInheritedChannelId == 0) {
if (client) { if (client) {
clientList.remove(client); clientList.remove(client);
} }
} else { } else {
if (client) { if (client) {
// Update client properties
// Other to the handleClientMoved function this handleClientPropertiesUpdate function also gets called
// if anything at all happend to the client, so we update the channel here to be sure we dont miss anything
client.channel = channelList.getById(data.payload.properties.channelGroupInheritedChannelId); client.channel = channelList.getById(data.payload.properties.channelGroupInheritedChannelId);
client.name = data.payload.properties.nickname; client.name = data.payload.properties.nickname;
client.inputMuted = data.payload.properties.inputMuted; client.inputMuted = data.payload.properties.inputMuted;
client.outputMuted = data.payload.properties.outputMuted; client.outputMuted = data.payload.properties.outputMuted;
} else { } else {
// For some reason the client did not exist in our list. Add client, to prevent further errors.
clientList.add( clientList.add(
new Client( new Client(
data.payload.clientId, data.payload.clientId,
@@ -63,6 +86,8 @@ function handleClientPropertiesUpdate(data) {
} }
} }
// Gets called when a client starts talking
//* NOTE: If the "current self-user" is speaking but muted, this will still be called. This does not apply to foreign clients
function handleTalkStatusChanged(data) { function handleTalkStatusChanged(data) {
let client = clientList.getById(data.payload.clientId); let client = clientList.getById(data.payload.clientId);
if (client) { if (client) {