Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (51 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
61 lines (51 loc) · 1.5 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
54
55
56
57
58
59
60
61
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
class Elevator {
constructor(io) {
this.currentFloor = 0;
this.requests = [];
this.moving = false;
this.io = io;
}
requestFloor(floor) {
this.requests.push(floor);
if (!this.moving) {
this.processRequests();
}
}
async processRequests() {
this.moving = true;
while (this.requests.length > 0) {
const nextFloor = this.requests.shift();
await this.moveToFloor(nextFloor);
}
this.moving = false;
}
async moveToFloor(floor) {
const travelTime = Math.abs(this.currentFloor - floor) * 1000; // 1 second per floor
console.log(`Moving from floor ${this.currentFloor} to floor ${floor}`);
this.io.emit('moving', { from: this.currentFloor, to: floor });
await new Promise(resolve => setTimeout(resolve, travelTime));
this.currentFloor = floor;
console.log(`Arrived at floor ${floor}`);
this.io.emit('arrived', { floor: this.currentFloor });
}
}
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const elevator = new Elevator(io);
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('New client connected');
socket.on('requestFloor', (floor) => {
elevator.requestFloor(floor);
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
});
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});