forked from johnpolacek/controldeck.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (56 loc) · 1.62 KB
/
app.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
/**
* Load the modules that we need
*/
var http = require('http'),
static = require('node-static'),
socketio = require("socket.io"),
dgram = require("dgram"),
url = require('url');
fs = require('fs');
/**
* Create a HTTP web server
*
* Port choice (8080) is arbitrary but should be distinct from the UDP
* listener port
*/
var app = http.createServer(handleRequest);
app.listen(8080);
/**
* Create a node-static server instance to serve the slide deck presentations
*/
var files = new static.Server('./public');
/**
* Create a Socket.IO server for communication between devices
*/
var io = socketio.listen(app);
/**
* Create a UDP network listener
*
* Port choice (43278) is arbitrary but should be distinct from the HTTP port
*/
var UDPsocket = dgram.createSocket('udp4');
UDPsocket.bind(43278);
/**
* Respond to a HTTP requests with the node-static server
*/
function handleRequest(req, res) {
// Log pathname
var pathname = url.parse(req.url).pathname;
console.log('pathname: '+pathname);
req.addListener('end', function () {
files.serve(req, res);
});
}
/**
* Respond to UDP messages
*
* When specific UDP messages are sent, this will pass commands to the
* slide decks through the Socket.IO server
*/
UDPsocket.on('message', function(content, rinfo) {
// Log the raw UDP message
console.log('got message', content, 'from', rinfo.address, rinfo.port);
// Convert the UDP message to a string and send it along to web
// application using Socket.IO
io.sockets.emit('message', content.toString());
});