-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
98 lines (88 loc) · 2.65 KB
/
server.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* jshint node: true */
'use strict';
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var robot = require('robotjs');
var config = require('./public/js/config.js');
var screenWidth = 1440;
var screenHeight = 900;
// Get screen size from NW desktop
try {
var gui = window.require('nw.gui');
gui.Screen.Init();
var screens = gui.Screen.screens;
// XXX: currently only take first screen
var rect = screens[0].bounds;
screenWidth = rect.width;
screenHeight = rect.height;
} catch (e) {
console.log(e);
}
var adjustment = 2;
var mouse = null;
var newX = null;
var newY = null;
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/client.html');
});
app.use('/public', express.static('public'));
io.on('connection', function(socket) {
socket.broadcast.emit('hi');
console.log('a user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
socket.on('mouse', function(pos) {
if (pos.pw) {
if (config.passcode !== pos.pw) {
return;
}
}
if (pos.cmd == 'move' || pos.cmd == 'scroll' || pos.cmd == 'drag') {
mouse = robot.getMousePos();
//console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);
newX = mouse.x + pos.x * adjustment;
newY = mouse.y + pos.y * adjustment;
//console.log('Offset is x:'+ newX + ' y:' + newY);
//robot.moveMouseSmooth(newX, newY);
robot.moveMouse(newX, newY);
mouse = robot.getMousePos();
//console.log("after x:" + mouse.x + " y:" + mouse.y);
} else if (pos.cmd == 'motion') {
var x = pos.x;
var y = pos.y;
x = (x < 45) ? 45 : x;
x = (x > 135) ? 135 : x;
y = (y < 105) ? 105 : y;
y = (y > 165) ? 165 : y;
x -= 45;
y -= 105;
robot.moveMouse(screenWidth / 90 * x, screenHeight / 60 * y);
} else if (pos.cmd == 'click') {
robot.mouseClick();
// robot.typeString(msg);
} else if (pos.cmd == 'rightclick') {
robot.mouseClick('right');
} else if (pos.cmd == 'scrollstart') {
robot.mouseToggle('down', 'middle');
} else if (pos.cmd == 'scrollend') {
robot.mouseToggle('up', 'middle');
} else if (pos.cmd == 'dragstart') {
robot.mouseToggle('down', 'left');
} else if (pos.cmd == 'dragend') {
robot.mouseToggle('up', 'left');
} else if (pos.cmd == 'right') {
robot.keyTap("right");
} else if (pos.cmd == 'left') {
robot.keyTap("left");
}
// send to everyone
//io.emit('mouse', pos);
});
});
var PORT = 8000;
http.listen(PORT, function() {
console.log('listening on *:' + PORT);
});