-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
58 lines (48 loc) · 1.34 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
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, net = require('net');
server.listen(8080);
io.set('log level', 1); // reduce logging
io.sockets.on('connection', function (socket) {
var client = null
var clientConnected = false
var that = this
socket.on('connectToRemote', function(data) {
var host = data.host || null
var port = data.port || null
if(host == null || port == null) {
socket.emit('error', createErrorPacket('Invalid remote params.'))
socket.close()
return
}
client = net.connect(port, host, function() {
clientConnected = true
console.log('connected!!!!!')
})
// register listeners
client.on('error', function(e) {
clientConnected = false
socket.emit('error', createErrorPacket('Remote connection error.', e))
socket.disconnect()
})
client.on('end', function() {
clientConnected = false
socket.emit('error', createErrorPacket('Remote connection end.'))
socket.disconnect()
})
})
socket.on('mouseMoveToPercent', function(data) {
console.log(data)
console.log(clientConnected)
if(clientConnected == true) {
client.write("x:" + data.x + ":y:" + data.y + ";")
}
})
});
var createErrorPacket = function(message, data) {
return {
message: message || "Unknown error",
data: data || null
}
}