-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (43 loc) · 1.39 KB
/
server.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
48
49
50
51
import { DEFAULT_FLOORS, DEFAULT_LIFTS } from "./lift-simulation.js"
import * as http from 'http';
import { Server } from "socket.io";
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const server = http.createServer(app);
const io = new Server(server)
const PORT = process.env.PORT || 8080
let clientCount = 0;
app.use(express.static(`${__dirname}/./client`));
const newConnection = () => {
io.emit('newClient',"new client joined...")
io.emit('liftGenerator',[DEFAULT_FLOORS, DEFAULT_LIFTS]) // if new client joins generate new lift simulation with default values for all
}
io.on('connection',(sock) => {
clientCount++;
sock.on('newClient',(text) => {
newConnection()
})
sock.on('liftGenerator',(event) => { //listening if any of the clients pressed generate
io.emit('liftGenerator',event)
})
sock.on('liftCall',(data) => {
io.emit('liftCall',data)
})
io.emit('clientCountUpdate',clientCount)
sock.on('disconnect',()=>{
clientCount--;
io.emit('clientCountUpdate',clientCount)
})
})
// errror handling server
server.on('error',(err) => {
console.error(err)
})
// port
server.listen(PORT,() => {
console.log(`${PORT} server is ready`)
})