Untitled
user_5663892656
plain_text
2 years ago
3.6 kB
4
Indexable
const app = require('express')(); const server = require('http').createServer(app); const cors = require('cors'); /** * @type {import('socket.io').Server} */ const io = require('socket.io')(server, { cors: { origin: '*', credentials: true, }, }); app.use(cors()); const listChannel = [ { id: '1', name: 'Channel 1', listUser: [ { id: '1', name: 'User 1', }, { id: '2', name: 'User 2', }, { id: '3', name: 'User 3', }, ], listActiveUser: [], }, { id: '2', name: 'Channel 2', listUser: [ { id: '1', name: 'User 1', }, { id: '2', name: 'User 2', }, ], listActiveUser: [], }, { id: '3', name: 'Channel 3', listUser: [ { id: '2', name: 'User 2', }, { id: '3', name: 'User 3', }, ], listActiveUser: [], }, ]; const listUser = [ { id: '1', name: 'User 1', }, { id: '2', name: 'User 2', }, { id: '3', name: 'User 3', }, { id: '4', name: 'User 4', }, ]; const PORT = process.env.PORT || 9999; app.get('/', (req, res) => { res.send('Running'); }); io.on('connection', (socket) => { console.log('New connection: ', socket.id); let currentChannel, currentUser; socket.on('joinChannel', ({ userId, channelId }) => { if ( !userId || !channelId || currentChannel?.listActiveUser?.includes((x) => x.id === userId) ) { console.log('out'); socket.disconnect(); return; } else { socket.join(channelId); currentChannel = listChannel.find((channel) => channel.id === channelId); currentUser = currentChannel.listUser.find((user) => user.id === userId); currentChannel.listActiveUser.push(currentUser); console.log('User joined: ', userId, channelId); // Return initial channel data socket.emit('acceptToChannel', currentChannel); // Emit to all user in channel that a new user joined socket.to(channelId).emit('userJoined', currentUser); } }); // User gửi signal socket.on('pairT', ({ isInitiator, from, to, channelId, signal }) => { console.log('pairT', isInitiator, from, to, channelId); // Chuyển signal cho các user khác nhận socket .to(channelId) .emit('pairT', { isInitiator, from, to, channelId, signal }); }); // User gửi signal socket.on('pairF', ({ isInitiator, from, to, channelId, signal }) => { console.log('pairF', isInitiator, from, to, channelId); // Chuyển signal cho các user khác nhận socket .to(channelId) .emit('pairF', { isInitiator, from, to, channelId, signal }); }); socket.on('leaveChannel', () => { console.log('User left: ', currentUser?.id, currentChannel?.id); socket.leave(currentChannel?.id); currentChannel.listActiveUser = currentChannel?.listActiveUser?.filter( (x) => x.id !== currentUser.id ); io.to(currentChannel.id).emit('userLeft', currentChannel); }); socket.on('disconnect', () => { console.log('User disconnected: ', currentUser?.id, currentChannel?.id); socket.leave(currentChannel?.id); if (currentChannel) { currentChannel.listActiveUser = currentChannel?.listActiveUser.filter( (x) => x.id !== currentUser.id ); io.to(currentChannel?.id).emit('userLeft', currentChannel); } }); }); server.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
Editor is loading...