-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
87 lines (74 loc) · 2.19 KB
/
index.ts
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
import WebSocket from 'ws';
import express from 'express';
import dotenv from 'dotenv';
dotenv.config();
let robot = require("robotjs");
let wsPort = getWsPort();
let httpPort = getHttpPort();
const wss = new WebSocket.Server({port: wsPort});
wss.on('connection', handleWs);
function handleWs(socket: WebSocket) {
socket.on('message', data => {
let obj = JSON.parse(data.toString());
switch (obj.type) {
case 'keypress':
robot.keyTap(obj.data);
break;
case 'keydown':
robot.keyToggle(obj.data, 'down');
break;
case 'keyup':
robot.keyToggle(obj.data, 'up');
break;
case 'mousemove':
let mouse = robot.getMousePos();
robot.moveMouse(mouse.x + obj.data.x * 3, mouse.y + obj.data.y * 3);
break;
case 'mouseclick':
robot.mouseClick(obj.data);
break;
case 'mousetoggle':
robot.mouseToggle(obj.data.up, obj.data.left);
break;
case 'scroll':
robot.scrollMouse(0, obj.data / 10);
break;
}
})
}
const app = express();
app.use(express.static('public'));
app.get('/ws-port', (_req, res) => {
res.send(wsPort.toString());
})
app.listen(httpPort, () => {
console.log(`Remote mouse started: http://localhost:${httpPort}`);
});
function getHttpPort(): number {
let httpPort: number;
try {
if(process.env.HTTP_PORT) {
httpPort = parseInt(process.env.HTTP_PORT);
} else {
httpPort = 8081;
}
} catch (e) {
console.error("ERROR: HTTP_PORT is not a number, falling back to default port (8081)");
httpPort = 8081;
}
return httpPort;
}
function getWsPort(): number {
let wsPort: number;
try {
if(process.env.WS_PORT) {
wsPort = parseInt(process.env.WS_PORT);
} else {
wsPort = 6942;
}
} catch (e) {
console.error("ERROR: WS_PORT is not a number, falling back to default port (6942)");
wsPort = 6942;
}
return wsPort;
}