-
Notifications
You must be signed in to change notification settings - Fork 2
/
viewer.js
57 lines (43 loc) · 1.23 KB
/
viewer.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
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
var ltime = 0;
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/jquery.js', function(req, res){
res.sendFile(__dirname + '/node_modules/jquery/dist/jquery.js');
});
app.get('/socket.io.js', function(req, res){
res.sendFile(__dirname + '/node_modules/socket.io-client/dist/socket.io.js');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.on('error', function(err) {
console.log(err);
server.close();
});
server.on('message', function(msg, rinfo) {
// throttle this down to about 20fps, browser has a hard time with more
var ctime = Date.now();
if (ctime - ltime > 25) {
ltime = ctime;
io.emit('pixels', msg);
}
// console.log(msg);
// console.log(rinfo);
});
server.on('listening', function() {
var address = server.address();
console.log('listening on ' + address.address + ':' + address.port);
});
server.bind(10420);