-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.19 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
45
46
47
const print = console.log
const path = require('path')
const port = process.env.PORT || 8080
const publicDirectoryPath = path.join(__dirname, '../public')
const express = require('express')
const http = require('http')
const app = express()
const server = http.createServer(app)
const io = require('socket.io').listen(server)
app.use(express.static('public'));
app.get('/', (req, res) => {
res.send('Chat Server is running on port 3000')
});
let c = 1
io.on('connection', (socket) => {
io.emit('userCount', 'total user: ' + c)
socket.on('deviceName', (message) => {
print(message)
})
socket.on('join', function(userNickname) {
c = c + 1
io.emit('userCount', 'total user: ' + c)
socket.broadcast.emit('userStatus', userNickname + " has joined the chat");
})
socket.on('chat', function(data) {
io.emit('chat', data);
});
socket.on('disconnect', function(userNickname) {
c = c - 1
if (c <= 0) c = 1
io.emit('userCount', 'total user: ' + c)
socket.broadcast.emit('userStatus', userNickname + "has left the chat");
})
})
server.listen(port, () => {
console.log('Node app is running on port 8080')
})