-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (74 loc) · 2.71 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
module.exports = function () {
/**
* Module dependencies.
*/
var path = require('path');
var express = require('express');
var routes = require(path.join(__dirname, 'routes'));
var http = require('http');
var config = require(path.join(process.cwd(), 'config'));
var fs = require('fs');
var async = require('async');
var orm = require('orm');
var app = express();
var routes = require(path.join(__dirname, 'lib', 'routes'));
require('./lib/models_register')(app);
// all environments
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(require('connect-multiparty')());
app.use(function(req, res, next){
if (req.is('text/*')) {
req.text = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ req.text += chunk });
req.on('end', next);
} else {
next();
}
});
app.use(routes.middleware(app));
app.use(express.methodOverride());
app.use(express.cookieParser(config.secret));
app.use(express.session());
app.set('views', path.join(process.cwd(), 'views'));
app.set('view engine', 'hjs');
app.locals.delimiters = '<% %>';
app.use(require('less-middleware')({ src: path.join(process.cwd(), 'public') }));
app.use(express.static(path.join(process.cwd(), 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
require(path.join(__dirname, 'lib', 'endpoints_register'))(app);
// require(path.join(__dirname, 'lib', 'views_register'))(app);
routes.register(app);
app.use(routes.unmatched(app));
var server = http.createServer(app);
server.listen(app.get('port'), function () {
console.log('Restlight server listening on port ' + app.get('port'));
});
var io = require('socket.io').listen(server, { log: false });
io.sockets.on('connection', function (socket) {
socket.on('set id', function (session_id) {
socket.set('id', session_id);
});
socket.on('send_message', function (session_id, message) {
var clients = io.sockets.clients();
for (var k in clients) {
var client = clients[k];
client.get('id', function (err, id) {
if (id == session_id) {
client.emit("msg", message);
}
});
}
});
socket.on('broadcast_message', function (message) {
io.sockets.emit('msg', message);
});
});
};