-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (54 loc) · 1.82 KB
/
server.js
File metadata and controls
71 lines (54 loc) · 1.82 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
62
63
64
65
66
67
68
69
import express from 'express';
import { createServer } from 'http';
import { Server as IOServer } from 'socket.io';
import path from 'path';
import { fileURLToPath } from 'url';
import BrowserController from './lib/browser.js';
import ControlQueue from './lib/queue.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const server = createServer(app);
const io = new IOServer(server);
app.use(express.static(path.join(__dirname, 'public')));
const browser = new BrowserController(io);
const queue = new ControlQueue(io);
io.on('connection', async socket => {
console.log('Client connected:', socket.id);
queue.add(socket.id);
socket.emit('queue-update', queue.list());
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
queue.remove(socket.id);
});
socket.on('control-event', event => {
if (queue.current() !== socket.id) return;
browser.handleInput(event);
});
socket.on('navigate', url => {
if (queue.current() !== socket.id) return;
browser.navigate(url);
});
socket.on('nav-back', () => {
if (queue.current() !== socket.id) return;
browser.handleButton('back');
});
socket.on('nav-forward', () => {
if (queue.current() !== socket.id) return;
browser.handleButton('forward');
});
socket.on('nav-refresh', () => {
if (queue.current() !== socket.id) return;
browser.handleButton('refresh');
});
socket.on("screen-size", size => {
browser.setClientResolution(size.w, size.h);
});
socket.on('release-control', () => {
queue.next();
});
});
server.listen(3000, async () => {
await browser.launch();
console.log('Server ready http://localhost:3000');
});