-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (28 loc) · 933 Bytes
/
server.js
File metadata and controls
39 lines (28 loc) · 933 Bytes
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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const PORT = process.env.PORT || 3000
http.listen(PORT, () => {
console.log(`Ready on port ${PORT}`)
})
app.use(express.static(__dirname + '/public'))
// socket
const users = {}
const typers = {}
io.on('connection', (socket) => {
socket.on('new user', (username) => {
users[socket.id] = username
socket.broadcast.emit('user connected', username)
})
socket.on('typing', (username) => {
socket.broadcast.emit('typing', username)
})
socket.on('send-chat-message', (message) => {
socket.broadcast.emit('chat-message', { message: message, username: users[socket.id] })
})
socket.on('disconnect', () => {
socket.broadcast.emit('user-disconnected', users[socket.id])
delete users[socket.id]
})
})