-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
67 lines (61 loc) · 1.66 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var mouse = require('node_mouse_mac');
var keyboard = require('node_keyboard_mac');
var httpServer = require('http').createServer(httpHandler);
var url = require('url');
var io = require('socket.io').listen(httpServer);
var fs = require('fs');
//var mime = require('mime');
httpServer.listen(8888);
io.set('heartbeats', false);
io.set('log level', 1);
io.sockets.on('connection',function(socket) {
//var lastmove = new Date();
socket.on('mouseMove', function(data) {
//var now = new Date();
console.log(data);
//if ((now - lastmove) > 1000) {
mouse.moveDelta(data.dx, data.dy);
mouse.show();
//lastmove = now;
//}
});
socket.on('mouseClick', function(data) {
console.log("CLICK!");
mouse.buttonDown();
setTimeout(mouse.buttonUp,50);
//mouse.buttonUp();
});
// need to add code to handle modifiers
// (shift, ctl, alt, cmd)
socket.on('keyPress', function(data) {
console.log("key!");
var code = Math.round(data.keyCode);
if(isNaN(code))
return;
//keyboard.keyDown(code);
//setTimeout(keyboard.keyUp, 50, code);
});
});
function httpHandler(req, res) {
var parsedUrl = url.parse(req.url, true);
if(parsedUrl.pathname === '/') {
parsedUrl.pathname = '/index.html';
}
if(fs.existsSync(__dirname + parsedUrl.pathname)) {
// serve file
//console.log(__dirname + parsedUrl.pathname);
fs.readFile(__dirname + parsedUrl.pathname, function(err, data) {
if(err) {
res.writeHead(500);
return res.end("Critical Error!");
}
//res.setHeader("Content-Type", mime.lookup(parsedUrl.pathname));
res.writeHead(200);
res.end(data);
});
} else {
// 404
res.writeHead(404, "Not found");
return res.end("Not Found");
}
}