forked from ishuah/Nodejs-MongoDb-TodoMVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·189 lines (164 loc) · 4.8 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, mongoose = require('mongoose')
, io = require('socket.io')
, mongoURI = process.env.MONGOLAB_URI || 'mongodb://localhost/todos'
, Schema = mongoose.Schema
, ObjectID = Schema.ObjectId
, Todo = require('./models/todos.js').init(Schema, mongoose)
;
var connectWithRetry = function() {
return mongoose.connect(mongoURI, function(err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
});
};
connectWithRetry();
mongoose.connection.on('open', function() {
console.log("connected to mongodb");
});
var app = express();
app.configure(function() {
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
});
var server = http.createServer(app).listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});
var sio = io.listen(server);
//User online user count variable
var users = 0;
var address_list = new Array();
sio.sockets.on('connection', function (socket) {
var address = socket.handshake.address;
if (address_list[address]) {
var socketid = address_list[address].list;
socketid.push(socket.id);
address_list[address].list = socketid;
} else {
var socketid = new Array();
socketid.push(socket.id);
address_list[address] = new Array();
address_list[address].list = socketid;
}
users = Object.keys(address_list).length;
socket.emit('count', { count: users });
socket.broadcast.emit('count', { count: users });
/*
handles 'all' namespace
function: list all todos
response: all todos, json format
*/
Todo.find({}, function(err, todos) {
socket.emit('all',todos);
});
/*
handles 'add' namespace
function: add a todo
Response: Todo object
*/
socket.on('add', function(data) {
var todo = new Todo({
title: data.title,
complete: false
});
todo.save(function(err) {
if (err) throw err;
socket.emit('added', todo );
socket.broadcast.emit('added', todo);
});
});
/*
Handles 'delete' namespace
function: delete a todo
response: the delete todo id, json object
*/
socket.on('delete', function(data) {
Todo.findById(data.id, function(err, todo) {
todo.remove(function(err) {
if (err) throw err;
socket.emit('deleted', data );
socket.broadcast.emit('deleted', data);
});
});
});
/*
Handles 'edit' namespace
function: edit a todo
response: edited todo, json object
*/
socket.on('edit', function(data) {
Todo.findById(data.id, function(err, todo){
todo.title = data.title;
todo.save(function(err){
if(err) throw err;
socket.emit('edited', todo);
socket.broadcast.emit('edited', todo);
});
});
});
/*
Handles 'changestatus' namespace
function: change the status of a todo
response: the todo that was edited, json object
*/
socket.on('changestatus', function(data) {
Todo.findById(data.id, function(err, todo) {
todo.complete = data.status == 'complete' ? true : false;
todo.save(function(err) {
if(err) throw err;
socket.emit('statuschanged', data );
socket.broadcast.emit('statuschanged', data);
});
});
});
/*
Handles 'allchangestatus' namespace
function: change the status of all todos
response: the status, json object
*/
socket.on('allchangestatus', function(data) {
var master_status = data.status == 'complete' ? true : false;
Todo.find({}, function(err, todos) {
for(var i = 0; i < todos.length; i++) {
todos[i].complete = master_status;
todos[i].save(function(err) {
if (err) throw err;
socket.emit('allstatuschanged', data);
socket.broadcast.emit('allstatuschanged', data);
});
}
});
});
//disconnect state
socket.on('disconnect', function() {
var socketid = address_list[address].list;
delete socketid[socketid.indexOf(socket.id)];
if(Object.keys(socketid).length == 0) {
delete address_list[address];
}
users = Object.keys(address_list).length;
socket.emit('count', { count: users });
socket.broadcast.emit('count', { count: users });
});
});
//Our index page
app.get('/', routes.index);