-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (38 loc) · 1.22 KB
/
server.js
File metadata and controls
48 lines (38 loc) · 1.22 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
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
app.listen(8000);
function handler (req, res) {
var startIndex = 1;
var lastIndex = req.url.indexOf('#');
if (lastIndex == -1) lastIndex = req.url.length;
var fileName = req.url.substring(startIndex, lastIndex);
fs.readFile(__dirname + '/' + fileName,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var terminal, left, right;
io.sockets.on('connection', function (socket) {
socket.on('terminal', function (data) {
terminal = socket;
});
socket.on('left', function (data) {
left = socket;
});
socket.on('right', function (data) {
right = socket;
});
socket.on('enable', function (data) {
if (left) left.emit('enable', data);
if (right) right.emit('enable', data);
});
socket.on('keystroke', function (data) {
if (terminal) terminal.emit('keystroke', data);
});
});