-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (38 loc) · 1.22 KB
/
server.js
File metadata and controls
43 lines (38 loc) · 1.22 KB
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
/**
* @file
* The control interface for the Raspberry Pi robot.
* Server listens on 8080 and returns static files in the 'public directory'.
*/
var io = require('socket.io'),
connect = require('connect'),
http = require('http');
// Set up the control interface server on 8080.
var controls = connect().use(connect.static('public')).listen(8080);
var responder = io.listen(controls);
responder.sockets.on('connection', function (socket) {
socket.emit('entrance', {message: 'Controller Engaged'});
socket.on('action', function (data) {
// When we get an 'action' event parse out the value and send it to the
// control API as a PUT.
var requestAction = JSON.parse(data).value;
console.log(requestAction);
var options = {
hostname: 'localhost',
port: 8888,
path: '/action',
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Connection' : 'close'
}
}
var apiRequest = http.request(options, function(res) {
console.log('STAT: ' + res.statusCode);
});
apiRequest.write(JSON.stringify({fire : requestAction}));
apiRequest.end();
apiRequest.on('error', function(e) {
console.log('hit an error');
});
});
});