-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushBoxServer.js
52 lines (44 loc) · 1.51 KB
/
PushBoxServer.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
/**
* Created by nick on 8/23/15.
*/
//HTTP Module used for client > server connections
var http = require('http');
//Ports used by the two network interfaces
const clientPort = 3001;
const endpointPort = 3000;
//Because console.log(); gets annoying
function cl(s){console.log(s);}
//Endpoint Interface
//------------------------------------------------
//Socket IO used for server > endpoint connections
var ioEndpoint = require('socket.io').listen(endpointPort);
//Server that connects to endpoints (raspberry pi)
ioEndpoint.sockets.on('connection', function (socket) {
cl("Endpoint Connected");
socket.on('disconnect', function() {
cl('Endpoint Disconnected');
});
});
//------------------------------------------------
//Client Interface
//------------------------------------------------
function handleRequest(request, response){
var seconds = new Date().getTime() / 1000;
var myDate = new Date(seconds * 1000);
console.log("Command |", request.url, "| sent at: ", myDate.toLocaleString());
//Send data to endpoint
ioEndpoint.emit('command', {
cmd: request.url
});
//Come back here and write a meaningful response sometime
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Ok");
response.write("\n");
response.end();
}
//HTTP Server that can be reached by a client
var ioClient = http.createServer(handleRequest);
ioClient.listen(clientPort, function(){
cl("Server Ready");
});
//------------------------------------------------