Spaces:
Running
Running
| 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, | |
| }; | |