Initial commit
This commit is contained in:
55
server/services/socketService.js
Normal file
55
server/services/socketService.js
Normal file
@@ -0,0 +1,55 @@
|
||||
function initializeSocketHandlers(io) {
|
||||
io.on('connection', (socket) => {
|
||||
console.log(`Client connected: ${socket.id}`);
|
||||
|
||||
// Join device-specific rooms for targeted updates
|
||||
socket.on('join_device_room', (deviceId) => {
|
||||
socket.join(`device_${deviceId}`);
|
||||
console.log(`Client ${socket.id} joined device room: device_${deviceId}`);
|
||||
});
|
||||
|
||||
// Join dashboard room for general updates
|
||||
socket.on('join_dashboard', () => {
|
||||
socket.join('dashboard');
|
||||
console.log(`Client ${socket.id} joined dashboard room`);
|
||||
});
|
||||
|
||||
// Leave rooms
|
||||
socket.on('leave_device_room', (deviceId) => {
|
||||
socket.leave(`device_${deviceId}`);
|
||||
console.log(`Client ${socket.id} left device room: device_${deviceId}`);
|
||||
});
|
||||
|
||||
socket.on('leave_dashboard', () => {
|
||||
socket.leave('dashboard');
|
||||
console.log(`Client ${socket.id} left dashboard room`);
|
||||
});
|
||||
|
||||
// Handle client disconnect
|
||||
socket.on('disconnect', () => {
|
||||
console.log(`Client disconnected: ${socket.id}`);
|
||||
});
|
||||
|
||||
// Send current status on connect
|
||||
socket.emit('connection_status', {
|
||||
status: 'connected',
|
||||
timestamp: new Date().toISOString(),
|
||||
clientId: socket.id
|
||||
});
|
||||
});
|
||||
|
||||
// Helper functions to emit events to specific rooms
|
||||
io.emitToDevice = function(deviceId, event, data) {
|
||||
io.to(`device_${deviceId}`).emit(event, data);
|
||||
};
|
||||
|
||||
io.emitToDashboard = function(event, data) {
|
||||
io.to('dashboard').emit(event, data);
|
||||
};
|
||||
|
||||
console.log('Socket.IO handlers initialized');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initializeSocketHandlers
|
||||
};
|
||||
Reference in New Issue
Block a user