-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (71 loc) · 2.09 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
68
69
70
71
72
73
74
75
76
var path = require('path');
var os = require('os');
var fs = require('fs');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var robot = require('robotjs');
var qr = require('qr-image');
var port = "8016";
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});
app.get('/server', function(req, res){
// 获取当前内网IP
var interfaces = os.networkInterfaces();
var localIp;
for(var devName in interfaces){
var interface = interfaces[devName];
for(var i = 0; i < interface.length; i++){
var alias = interface[i];
if(interface[i].family == 'IPv4'
&&
interface[i].address !== '127.0.0.1'
&&
!interface[i].internal
&&
interface[i].address.substr(0, 7) == '192.168'){
localIp = interface[i].address;
}
}
};
var url = 'http://' + localIp + ':' + port;
// 生成二维码
var qrStream = qr.imageSync(url);
fs.writeFileSync(path.join(__dirname, 'public/images/local-ip.png'), qrStream);
res.sendFile(path.join(__dirname, 'views/server.html'));
});
// 启动服务
http.listen(port, function(){
console.log('已开始web服务, Port: 8016');
});
io.on('connection', function(socket){
console.log('已初始化 socket 连接');
socket.emit('ready', {status: 'ready ...'});
socket.on('setMouse', function(data){
console.log(data);
var prevMousePos = robot.getMousePos();
switch(data.action){
case "up":
robot.moveMouse(prevMousePos.x, prevMousePos.y - 10);
break;
case "down":
robot.moveMouse(prevMousePos.x, prevMousePos.y + 10);
break;
case "right":
robot.moveMouse(prevMousePos.x + 10, prevMousePos.y);
break;
case "left":
robot.moveMouse(prevMousePos.x - 10, prevMousePos.y);
break;
case "click":
robot.mouseClick();
break;
}
});
socket.on('disconnect', function(){
console.log('已断开 socket 连接');
});
});