-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
159 lines (143 loc) · 4.17 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
// Node Module dependencies:
var express = require('express');
var http = require('http');
var path = require('path');
// Application specific requires:
var index = require('./routes/index');
var user = require('./routes/user');
var feed = require('./routes/feed');
var chat = require('./routes/chat');
var search = require('./routes/search');
var viewHelpers = require('./views/helpers');
var notifications = require('./routes/notifications');
var Notification = require('./db/Notification');
// Set up Express, socket.io
app = express(); //global app object
var flash = require('connect-flash');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(
function()
{
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon(__dirname + "/public/images/favicon.ico"));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(flash());
app.use(function(req, res, next){ res.locals.current_user = req.session.user; next(); });
app.use(function(req, res, next){ res.locals.message = req.flash(); next(); });
app.use(checkNotifications);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(Handle404);
app.locals.helpers = viewHelpers;
}
);
function checkNotifications(req, res, next)
{
res.locals.notifications = [];
if(req.session.user)
{
Notification.getNotifications(req.session.user, function(rows){
res.locals.notifications = rows;
// console.log(rows);
next();
});
}
else
{
next();
}
}
//Handles 404 page
function Handle404(req, res, next)
{
res.render('404');
}
//error handling
app.configure('development',
function()
{
app.use(express.errorHandler());
}
);
//Tells the console it is listening on the given port
server.listen(app.get('port'),
function()
{
console.log("Express server listening on port " + app.get('port'));
}
);
//array of connected users
var connectedClients = [];
io.set("log level", 1);
io.sockets.on("connection", function(socket)
{
console.log("new connection:");
//console.log(socket);
// socket.emit("message", {message: "hello from server", from: "Server"});
socket.on("set nickname", function(username)
{
socket.set("nickname", username);
connectedClients[username] = socket;
console.log("CLIENTS: ");
console.log(connectedClients);
});
socket.on("echo", function(data)
{
console.log("echo received");
console.log(data);
io.sockets.emit("message", {message: "ECHO: "+data, sender: "Server"});
});
socket.on("relay", function(data)
{
// console.log("relay");
// console.log(data);
// var tmp = io.sockets.sockets[data.username];
// console.log(tmp);
// console.log(io.sockets);
// console.log(io.sockets.sockets);
var soc = connectedClients[data.to];
if(!soc){
//TODO: emit("error")
socket.emit("message", {message: "ERROR: USER NOT CONNECTED", from: "SERVER"});
}
else{
soc.emit("message", data);
}
//io.sockets.emit("message", {message: data, sender: "Server"});
});
socket.on("disconnect", function()
{
console.log("client disconnect");
});
});
// Define routes, route handlers
app.get('/', index.index);
app.get('/signup', index.signup);
app.get('/:user/profile', user.profile);
app.get('/:user/followers', user.followers);
app.get('/:user/following', user.following);
app.get('/hashtag/:hashtag', feed.hashtag);
app.get('/language/:language', feed.language);
app.get('/chat', chat.index);
app.get('/help', index.help);
app.get('/settings', index.settings);
app.get('/shorty', index.shorty);
app.get('/:user/edit', user.edit);
app.delete('/:user/delete', user.delete);
app.get('/signout', index.signout);
app.post('/signin', index.signin);
app.post('/toggleFollow', index.toggleFollow);
app.post('/addNewUser', index.createNewUser);
app.post('/submitNewPost', index.submitNewPost);
app.post('/search', search.searchAll);
app.post('/search/users', search.searchUsers);
app.get('/search', search.searchAll);
app.put('/update', user.update);
app.post('/dismiss_notification', notifications.remove);