From 1982c6f20d01a07d9a4cfc0754f5c40b03cd7f70 Mon Sep 17 00:00:00 2001 From: Brandon Perard Date: Fri, 10 Nov 2023 16:34:19 -0800 Subject: [PATCH] ADD messageHandlers, UPDATE code for deploy --- handlers/userHandlers.js | 2 +- index.js | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/handlers/userHandlers.js b/handlers/userHandlers.js index dd00ece..4c8ad7c 100644 --- a/handlers/userHandlers.js +++ b/handlers/userHandlers.js @@ -130,7 +130,7 @@ const userHandlers = (server, socket) => { socket.on('USER:GET_ROOM_INFO', getRoomInfo); socket.on('USER:GET_ALL_ROOM_INFO', getAllRoomInfo); - socket.on('USER:NAME_ADD', addUserName); + socket.on('USER:NAME_ADDED', addUserName); socket.on('USER:NAME_REMOVED', removeUserName); socket.on('USER:NAME_UPDATED', updateUserName); }; diff --git a/index.js b/index.js index 00f93db..2b042a4 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,52 @@ const onConnection = (socket) => { // HANDLERS registerUserHandlers(server, socket); + messageHandlers(server, socket); }; server.on('connection', onConnection); + + +const messageHandlers = (server, socket) => { + // --- MESSAGE HANDLERS --- + + const sendMessage = (message) => { + const newMessage = { + MID: `${socket.id}${Date.now()}`, + SID: socket.id, + message, + }; + server.emit('MESSAGE:ADD', newMessage); + }; + + const deleteMessage = (MID) => { + server.emit('MESSAGE:DELETE', MID); + }; + + const messageReply = (originMID, message) => { + const replyMessage = { + originMID, + MID: `${socket.id}${Date.now()}`, + SID: socket.id, + message, + }; + server.emit('MESSAGE:REPLY', replyMessage); + }; + + const messageReaction = (originMID, reaction) => { + const messageReaction = { + originMID, + MID: `${socket.id}${Date.now()}`, // can remove if one reaction per originMID + SID: socket.id, + reaction, // provided by the client, reference library kept in channel, send UID for any reaction to find reaction + }; + server.emit('MESSAGE:REACTION', messageReaction); + }; + + // --- LISTENERS - USER:(EVENT_NAME) --- + + socket.on('MESSAGE:ADD', sendMessage); + socket.on('MESSAGE:DELETE', deleteMessage); + socket.on('MESSAGE:REPLY', messageReply); + socket.on('MESSAGE:REACTION', messageReaction); +}; \ No newline at end of file