mirror of
https://github.com/sergeyyarkov/react-chat-app.git
synced 2026-10-11 10:29:04 +03:00
22 lines
730 B
JavaScript
22 lines
730 B
JavaScript
module.exports = {
|
|
start: (io) => {
|
|
io.on('connection', (socket) => {
|
|
socket.on('user:connected', ({ username }) => {
|
|
users.push({ username, socketId: socket.id })
|
|
io.emit('user:connected', { username, online: users.length })
|
|
})
|
|
|
|
socket.on('chat:message', ({ message, username, date }) => {
|
|
io.emit('chat:message', { message, username, date })
|
|
})
|
|
|
|
socket.on('disconnecting', () => {
|
|
const userIndex = users.findIndex((user) => socket.id === user.socketId)
|
|
if (userIndex !== -1) {
|
|
io.emit('user:disConnected', { username: users[userIndex].username, online: users.length - 1 })
|
|
users.splice(userIndex, 1)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
} |