-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdancefloor.js
53 lines (44 loc) · 1.44 KB
/
dancefloor.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
52
53
const request = require('request-promise-native');
const util = require('util');
const net = require('net');
const os = require('os');
let screen;
module.exports.init = async (config, initialScreen) => {
screen = initialScreen;
console.log('Creating server...');
const server = net.createServer();
const listen = util.promisify(server.listen).bind(server);
await listen();
const host = getIpAddress();
if (!host) {
throw new Error('Could not determine local IP address');
}
const {port} = server.address();
console.log(`Asking dancefloor at ${config.host}:${config.httpPort} to delegate to ${host}:${port}...`);
await request.post(`http://${config.host}:${config.httpPort}/api/delegate`, {
json:true,
body: {
host,
port
}
});
console.log('Awaiting connections...');
server.on('connection', socket => {
console.log('Got connection. Waiting for data...');
socket.on('data', (data) => {
const [width, height] = data;
screen.render(width, height, socket);
});
});
}
const getIpAddress = () => {
const ifaces = os.networkInterfaces();
for (let name of Object.keys(ifaces)) {
for (iface of ifaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
}
module.exports.setScreen = (value) => screen = value;