From 601d7b830298c9a079c0f0396abfa30b4d895fd7 Mon Sep 17 00:00:00 2001 From: Janis Date: Wed, 11 Jan 2023 13:46:27 +0100 Subject: [PATCH] cleaned event_handlers.js --- js/event_handlers.js | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/js/event_handlers.js b/js/event_handlers.js index 09082d5..1bd13a4 100644 --- a/js/event_handlers.js +++ b/js/event_handlers.js @@ -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) { + // Set channels and clients channelList.setItems(parseChannelInfos(data.payload.connections[0].channelInfos)); clientList.setItems(parseClientInfos(data.payload.connections[0].clientInfos)); + + // The client of this current user 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) { + // 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); if (data.payload.newChannelId == 0) { - // User disconnected + // If newChannelId is 0, the client left the server + // Client disconnected if (client) { 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) { + //* 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"); - clientList.clear(); + clientList.clear(); // remove all clients. } } else { - // User moved channel + // Client switched the channel OR JOINED the server to a channel if (client) { - // Client already exists in list - clientList.getById(data.payload.clientId).channel = channelList.getById(data.payload.newChannelId); + // Client just switched the channel + // 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 { - console.log(data.payload); - // New Client has to be created + // Client joined the server + // Like described at the const client declaration, the client is null he is NEW in our list/server clientList.add( new Client( 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) { - 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 (client) { clientList.remove(client); } } else { 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.name = data.payload.properties.nickname; client.inputMuted = data.payload.properties.inputMuted; client.outputMuted = data.payload.properties.outputMuted; } else { + // For some reason the client did not exist in our list. Add client, to prevent further errors. clientList.add( new Client( 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) { let client = clientList.getById(data.payload.clientId); if (client) {