Spaces:
Running
Running
File size: 650 Bytes
94361b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | const socketIo = require('socket.io');
let io;
const init = (server) => {
io = socketIo(server, {
cors: {
methods: ['GET', 'POST'],
credentials: true,
},
});
io.on('connection', (socket) => {
console.log('New client connected');
socket.on('join', (userId) => {
console.log(`User ${userId} joined their own room`);
socket.join(userId);
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
return io;
};
const getIO = () => {
if (!io) {
throw new Error('Socket.io not initialized!');
}
return io;
};
module.exports = {
init,
getIO,
};
|