-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
59 lines (50 loc) · 1.4 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
var http = require('http');
var static = require('node-static');
var file = new(static.Server)('.');
var net = require('net');
var tcpClients = [];
// Create HTTP server
var serv = http.createServer(function (req, res) {
console.log('HTTP Connection');
req.addListener('end', function () {
// Serve files!
file.serve(req, res);
});
}).listen(1337, '127.0.0.1');
// Initialize Socket.io
var io = require('socket.io').listen(serv);
io.set('log level',1);
//Pass socket.io events to TCP streams.
io.sockets.on('connection', function(socket) {
socket.on('ledOn',function(data) {
console.log(data);
tcpClients[0].write(JSON.stringify(data));
});
socket.on('ledOff',function(data) {
tcpClients[0].write(JSON.stringify(data));
console.log(data);
});
});
// Pass TCP events to socket.io
function heartbeat(data) {
io.sockets.emit('board',data);
}
// Handle TCP client connections.
var server = net.createServer(function(c) { //'connection' listener
console.log('TCP client connected');
c.on('end', function() {
console.log('TCP client disconnected');
tcpClients.pop();
});
c.on('data', function(data) {
var data = JSON.parse(data.toString());
console.log(data);
heartbeat(data);
});
//c.pipe(c);
tcpClients.push(c);
});
server.listen(8124, function() {
console.log('TCP server bound');
});
console.log('Server running at http://127.0.0.1:1337/');