-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (64 loc) · 1.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var express = require('express'),
path = require('path'),
app = express(),
router = express.Router(),
qrCode = require('qrcode-npm/qrcode'),
morgan = require('morgan'),
opts = require('opts');
app.use(morgan());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use('/static', express.static(__dirname + '/static'));
app.use('/', router);
app.get('/', function (req, res) {
res.render('index');
});
app.get('/box', function (req, res) {
res.render('box');
});
opts.parse([
{
'short': 'p',
'long': 'port',
'description': 'HTTP port',
'value': true,
'required': false
},
]);
var port = process.env.PORT || opts.get('port') || 3000
var server = app.listen(port, function() {
console.log('Listening on port %d...', server.address().port);
});
app.get('/qr', function (req, res) {
var size = 4;
var qr = qrCode.qrcode(4, 'M');
qr.addData(req.headers.host);
qr.make();
if (req.query.s && isFinite(req.query.s)) {
size = +req.query.s;
if (size > 20) size = 20;
if (size < 1 ) size = 1;
}
res.render('qr', { imgTag: qr.createImgTag(size) });
});
var io = require('socket.io').listen(server, {'log level': 1});
io.configure(function() {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 20);
});
io.sockets.on('connection', function(socket) {
socket.on('dummy', function(data) { console.log('dummy'); });
socket.on('nagesen', function(data) {
console.log('handle_nagesen', data);
val_type = '';
if (typeof data === 'string') {
val_type = data;
} else if ( data && typeof data === 'object' ) {
val_type = data['val_type'];
}
if ( val_type ) {
console.log('throw nagesen type:', val_type);
socket.broadcast.emit('box', { 'val_type': val_type });
}
});
});