IO-2924-Refactor-Production-board-to-use-Socket-Provider: Finalize
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -11,32 +11,50 @@ function createLogEvent(socket, level, message) {
|
||||
logger.log("ioredis-log-event", level, socket.user.email, null, { wsmessage: message });
|
||||
}
|
||||
|
||||
const redisSocketEvents = (io, { addUserToRoom, getUsersInRoom, removeUserFromRoom }) => {
|
||||
const registerUpdateEvents = (socket) => {
|
||||
socket.on("update-token", async (newToken) => {
|
||||
try {
|
||||
socket.user = await admin.auth().verifyIdToken(newToken);
|
||||
createLogEvent(socket, "INFO", "Token updated successfully");
|
||||
socket.emit("token-updated", { success: true });
|
||||
} catch (error) {
|
||||
createLogEvent(socket, "ERROR", `Token update failed: ${error.message}`);
|
||||
socket.emit("token-updated", { success: false, error: error.message });
|
||||
// Optionally disconnect the socket if token verification fails
|
||||
socket.disconnect();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const redisSocketEvents = (io, { addUserToRoom, getUsersInRoom, removeUserFromRoom }, { getBodyshopRoom }) => {
|
||||
// Room management and broadcasting events
|
||||
function registerRoomAndBroadcastEvents(socket) {
|
||||
socket.on("join-bodyshop-room", async (bodyshopUUID) => {
|
||||
socket.join(bodyshopUUID);
|
||||
await addUserToRoom(bodyshopUUID, { uid: socket.user.uid, email: socket.user.email });
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
socket.join(room);
|
||||
await addUserToRoom(room, { uid: socket.user.uid, email: socket.user.email });
|
||||
createLogEvent(socket, "DEBUG", `Client joined bodyshop room: ${bodyshopUUID}`);
|
||||
|
||||
// Notify all users in the room about the updated user list
|
||||
const usersInRoom = await getUsersInRoom(bodyshopUUID);
|
||||
io.to(bodyshopUUID).emit("room-users-updated", usersInRoom);
|
||||
io.to(room).emit("room-users-updated", usersInRoom);
|
||||
});
|
||||
|
||||
socket.on("leave-bodyshop-room", async (bodyshopUUID) => {
|
||||
socket.leave(bodyshopUUID);
|
||||
createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${bodyshopUUID}`);
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
socket.leave(room);
|
||||
createLogEvent(socket, "DEBUG", `Client left bodyshop room: ${room}`);
|
||||
});
|
||||
|
||||
socket.on("get-room-users", async (bodyshopUUID, callback) => {
|
||||
const usersInRoom = await getUsersInRoom(bodyshopUUID);
|
||||
const usersInRoom = await getUsersInRoom(getBodyshopRoom(bodyshopUUID));
|
||||
callback(usersInRoom);
|
||||
});
|
||||
|
||||
socket.on("broadcast-to-bodyshop", async (bodyshopUUID, message) => {
|
||||
io.to(bodyshopUUID).emit("bodyshop-message", message);
|
||||
createLogEvent(socket, "INFO", `Broadcast message to bodyshop ${bodyshopUUID}`);
|
||||
const room = getBodyshopRoom(bodyshopUUID);
|
||||
io.to(room).emit("bodyshop-message", message);
|
||||
createLogEvent(socket, "INFO", `Broadcast message to bodyshop ${room}`);
|
||||
});
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
@@ -45,12 +63,12 @@ const redisSocketEvents = (io, { addUserToRoom, getUsersInRoom, removeUserFromRo
|
||||
// Get all rooms the socket is part of
|
||||
const rooms = Array.from(socket.rooms).filter((room) => room !== socket.id);
|
||||
|
||||
for (const bodyshopUUID of rooms) {
|
||||
await removeUserFromRoom(bodyshopUUID, { uid: socket.user.uid, email: socket.user.email });
|
||||
for (const bodyshopRoom of rooms) {
|
||||
await removeUserFromRoom(bodyshopRoom, { uid: socket.user.uid, email: socket.user.email });
|
||||
|
||||
// Notify all users in the room about the updated user list
|
||||
const usersInRoom = await getUsersInRoom(bodyshopUUID);
|
||||
io.to(bodyshopUUID).emit("room-users-updated", usersInRoom);
|
||||
const usersInRoom = await getUsersInRoom(bodyshopRoom);
|
||||
io.to(bodyshopRoom).emit("room-users-updated", usersInRoom);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -61,7 +79,7 @@ const redisSocketEvents = (io, { addUserToRoom, getUsersInRoom, removeUserFromRo
|
||||
|
||||
// Register room and broadcasting events
|
||||
registerRoomAndBroadcastEvents(socket);
|
||||
|
||||
registerUpdateEvents(socket);
|
||||
// Handle socket disconnection
|
||||
socket.on("disconnect", async () => {
|
||||
createLogEvent(socket, "DEBUG", `User disconnected.`);
|
||||
|
||||
Reference in New Issue
Block a user