-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
42 lines (31 loc) · 1.01 KB
/
index.js
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
41
42
const app = require('express')()
const http = require('http')
const {Server} = require('socket.io')
const server = http.createServer(app)
const io = new Server(server)
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html')
})
io.on('connection', function(socket){
const otherNickname = socket.handshake.query.nickname
const userCredential = {
otherNickname,
id: socket.id
}
socket.broadcast.emit('other user connected', {...userCredential})
socket.on('disconnect', function(){
socket.broadcast.emit('other user disconnected', {...userCredential})
})
socket.on('chat message', function(msg){
socket.broadcast.emit('chat message', `[ ${otherNickname} ]: ${msg}`)
})
socket.on('someone is typing', function(){
socket.broadcast.emit('someone is typing', {...userCredential})
})
socket.on('someone finish typing', function(){
socket.broadcast.emit('someone finish typing', {...userCredential})
})
})
server.listen(process.env.PORT || 3000, function(){
console.log('listening on *:3000')
})