-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
67 lines (48 loc) · 1.58 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
var mongoose = require('mongoose')
, express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server, { origins: '*:*', log: !!process.env.SOCKET_IO_LOG })
, debug = require('debug')('lelylan');
// *************
// Express app
app.configure(function() {
app.use(express.static(__dirname + '/app/assets/javascripts'))
app.use(express.static(__dirname + '/app/assets'))
});
// index test page
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html')
});
// websocket fire test page
app.put('/test', function(request, response) {
io.sockets.in('token-1').emit('update', { data: require('./test/fixtures/device.json') });
response.json({});
});
// ***************
// Websockets app
io.set('origins', '*:*' );
io.sockets.on('connection', function(socket) {
socket.on('subscribe', function(room) {
debug('Subscribing room', room);
socket.join(room); // a room is equivalent to an access token
});
socket.emit('connected');
});
server.listen(process.env.PORT, function() {
debug('Websocket worker up and running on port', process.env.PORT);
// if run as root, downgrade to the owner of this file
if (process.getuid() === 0)
require('fs').stat(__filename, function(err, stats) {
if (err) return console.log(err)
process.setuid(stats.uid);
});
});
server.listen(process.env.PORT);
// ***************
// Realtime loop
var _loop = require('./lib/loop');
_loop.execute(io);
// ***************
// App interface
module.export = { io: io, app: app };