-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (43 loc) · 1.63 KB
/
Copy pathapp.js
File metadata and controls
53 lines (43 loc) · 1.63 KB
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
48
49
50
51
52
53
const http = require('http')
const express = require('express')
const path = require('path')
const socketio = require('socket.io')
const app = express()
const server = http.createServer(app) //io requires raw http
const io = socketio(server)
const publicDirectoryPath = path.join(__dirname, './public')
app.use(express.static(publicDirectoryPath))
let peopleCount = 0;
let peopleLimit = 30;
let peopleEnterable = 0;
io.on('connection', socket => {
//socket.broadcast.emit("showMessage", { name: 'Anonymous', message: 'A NEW USER HAS JOINED' });
//socket.broadcast.emit("peopleCount", { count: '+', peopleCount : peopleCount, peopleLimit : peopleLimit, warning : false});
socket.on('sendMessage', message => io.emit('showMessage', message))
socket.on('peopleCount', (data) => {
if(data.count == '+'){
peopleCount++;
}else if (data.count == '-'){
peopleCount--;
}
data.peopleCount = peopleCount;
if(data.peopleLimit != undefined){
peopleLimit = data.peopleLimit;
data.peopleLimit = peopleLimit;
}else{
data.peopleLimit = peopleLimit;
}
console.log(data.peopleLimit)
if(peopleCount >= peopleLimit){
data.warning = true;
data.peopleEnterable = 0;
}else{
data.peopleEnterable = (peopleLimit - peopleCount);
console.log("data.peopleEnterable = " + data.peopleEnterable);
data.warning = false;
}
io.emit("peopleCount", data);
});
})
const port = process.env.PORT || 3000
server.listen(port, () => console.log('Server is running...'))