-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (32 loc) · 1.03 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
43
44
let app = require('express')();
let http = require('http').Server(app);
let io = require('socket.io')(http);
process.env.PORT=80;
app.get('/', (req, res) => {
return res.json("socket server connected");
})
io.on('connection', (socket) => {
io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
socket.on('disconnect', () => {
console.log('disconnected')
io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
})
socket.on('chat-message', (msg) => {
socket.broadcast.emit('chat-message', msg)
})
socket.on('joined', (name) => {
socket.broadcast.emit('joined', name)
})
socket.on('leaved', (name) => {
socket.broadcast.emit('leaved', name)
})
socket.on('typing', (data) => {
socket.broadcast.emit('typing', data)
})
socket.on('stoptyping', () => {
socket.broadcast.emit('stoptyping')
})
})
http.listen(process.env.PORT, () => {
console.log('Server is started at http://localhost:'+process.env.PORT)
})