Skip to content

Commit

Permalink
Merge pull request #23 from bperard/dev-user-handlers
Browse files Browse the repository at this point in the history
ADD messageHandlers, UPDATE code for deploy
  • Loading branch information
bperard authored Nov 12, 2023
2 parents 7641b7d + 1982c6f commit 247966f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion handlers/userHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 247966f

Please sign in to comment.