-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
152 lines (125 loc) · 4.05 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//dependencies
var express = require('express')
, mongoStore = require('connect-mongo')(express)
, http = require('http')
, path = require('path')
, passport = require('passport')
, mongoose = require('mongoose')
, socketIo = require('socket.io')
, passportSocketIo = require("passport.socketio")
, Eventbrite = require('eventbrite');
//create express app
var app = express();
//mongo uri
app.set('mongodb-uri', process.env.MONGOLAB_URI || 'localhost/starter');
//setup mongoose
app.db = mongoose.createConnection(app.get('mongodb-uri'));
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
console.log('mongoose open for business');
});
//config data models
require('./models')(app, mongoose);
//config passport
require('./passport')(app, passport);
//config all
app.configure(function(){
//settings
app.disable('x-powered-by');
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('strict routing', true);
app.set('project-name', "nerdclustr");
mySessionStore = new mongoStore({ url: app.get('mongodb-uri') });
mySecret = 'Sup3rS3cr3tK3y';
//middleware
app.use(express.favicon(__dirname + '/public/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: mySecret,
store: mySessionStore
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
//locals
app.locals.projectName = app.get('project-name');
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.get('company-name');
});
//config dev
app.configure('development', function(){
app.use(express.errorHandler());
});
//route requests
require('./routes')(app);
//utilities
require('./utilities')(app);
//listen up
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = require('socket.io').listen(server);
io.set('loglevel',10) // set log level to get all debug messages
io.set("authorization", passportSocketIo.authorize({
key: 'connect.sid', //the cookie where express (or connect) stores its session id.
secret: mySecret, //the session secret to parse the cookie
store: mySessionStore, //the session store that express uses
fail: function(data, accept) { // *optional* callbacks on success or fail
console.log('fail');
accept(null, false); // second param takes boolean on whether or not to allow handshake
},
success: function(data, accept) {
console.log('success');
accept(null, true);
}
}));
// Clients is a list of users who have connected
var clients = [];
var count = 0;
///////////////////////////////////////////////////////////////////// SEND() UTILITY
function send(message) {
clients.forEach(function(client) {
client.send(message);
});
}
io.on('connection',function(socket){
count++;
clients.push(socket);
io.sockets.emit('count', {
number: count
});
// var nerdtype = .....
// switch(nerdtype){
// case developer:
// console.log('emit developer count');
// io.sockets.emit('count', {
// number: count
// });
// break;
// case designer:
// console.log('emit designer count');
// break;
// case manager:
// console.log('emit manager count');
// break;
// default:
// console.log('user is of no nerdtype');
// };
socket.on('send:coords', function (data) {
socket.broadcast.emit('load:coords', data);
});
socket.on('disconnect', function (client) {
var index = clients.indexOf(client);
clients.splice(index,1);
count--;
io.sockets.emit('count', {
number: count
});
});
})