forked from indyblue/android_remote_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (85 loc) · 2.79 KB
/
app.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
'option strict';
var WebSocketServer = require('ws').Server
, http = require('http')
, fs = require('fs')
, ptool = require('path')
, minicap = require('./minicap')
, minitouch = require('./minitouch')
, minisvc = require('./miniservice')
// , minitether = require('./minitether')
, mini0 = require('./mini0')
, { addPort, listPorts } = require('./mini0');
var PORT = process.env.PORT || 9002
const page = ptool.join(__dirname, 'index.html');
function requestListener(req, res) {
console.log('request', req.url);
var fstream = fs.createReadStream(page);
fstream.pipe(res);
};
var server = http.createServer(requestListener);
var wss = new WebSocketServer({ server: server });
var sockets = mini0.webSockets;
wss.on('connection', function (ws) {
ws.sendObj = o => ws.send(JSON.stringify(o));
sockets.push(ws);
console.info('Got a client');
minicap.sendLastFrame();
ws.on('message', function (d) {
//console.log(d);
let j = null;
try { j = JSON.parse(d); } catch (e) { }
if (j) {
if (j.event === 'mouse') minitouch.action(j.t, j.x, j.y);
else if (j.event === 'power') minisvc.doPower();
else if (j.event === 'minicap') minicap.toggle();
else if (j.event === 'key') minisvc.onKey(j.key, j.mods, j.isDown);
else if (j.event === 'getclip') minisvc.getClip().then(msg =>
ws.sendObj(msg));
else if (j.event === 'setclip') minisvc.setClip(j.text);
else if (j.event === 'listports')
ws.sendObj({ type: 'ports', data: listPorts() });
else if (j.event === 'addport') {
for (var p of j.port.split(',')) addPort(p, j.rev);
ws.sendObj({ type: 'ports', data: listPorts() });
}
else if (j.event === 'debug') {
console.log('debug', j.val);
minicap.debug = j.val;
// minitouch.debug = j.val;
}
}
});
ws.on('close', function () {
var idx = sockets.indexOf(ws);
if (idx >= 0) sockets.splice(idx, 1);
console.info('lost a client: ' + idx);
});
minicap.start();
minisvc.cbRotate = () => minicap.restart(true);
});
server.listen(PORT);
console.info('Listening on port %d', PORT);
process.stdin.setEncoding('utf8');
process.stdin.resume();
process.stdin.on('data', d => {
d = d.trim();
console.log(d, d === 'q');
if (d === 'q') process.exit();
});
const onExit = () => {
console.log('stopping server', PORT);
minicap.onExit();
// minitouch.onExit();
minisvc.onExit();
// minitether.onExit();
server.close();
mini0.onExit();
if (process.argv.indexOf('-u') < 0) {
console.log('*** TO FINISH UNINSTALL ***');
console.log(`adb uninstall jp.co.cyberagent.stf`);
console.log(`cd ./gnirehtet; ./gnirehtet uninstall`);
}
};
process.on('exit', onExit);
process.on('SIGTERM', () => process.exit(1));
process.on('SIGINT', () => process.exit(1));