-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
143 lines (118 loc) · 4.55 KB
/
server.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
//Global
require('./server/global/global.js');
//Includes
var express = require('express');
var http = require('http');
var db = require('./server/db/db.js');
var bodyParser = require('body-parser');
var opener = require('opener');
//Create app
var app = express();
//Create server and listen to it
var server = http.createServer(app);
io = require('socket.io').listen(server);
server.listen(config.PORT);
logger.info("Server listening on port " + config.PORT);
//Initialize set of loggedInUsers
var loggedInUsers = {};
//Initialize path variables for public and other packages
app.use(express.static(__dirname + '/public'));
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'));
app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/'));
app.use('/angular', express.static(__dirname + '/node_modules/angular/'));
app.use('/angular-route', express.static(__dirname + '/node_modules/angular-route/'));
app.use('/angular-cookies', express.static(__dirname + '/node_modules/angular-cookies/'));
app.use('/font-awesome', express.static(__dirname + '/node_modules/font-awesome/'));
app.use('/moment', express.static(__dirname + '/node_modules/moment/min/'));
//Create JSON parser to read from request
var jsonParser = bodyParser.json();
//API Handlers
//Return Index.html
app.get('/', function(req, res) {
res.sendFile(__dirname + "/public/" + "index.html");
});
/**
* API Handler to authenticate the username, password of the user while logging in
* @param {[type]} req [login request with username and password]
* @return {[type]} [result user object with fullName, userId, contactPicture]
*/
app.post('/api/authenticate', jsonParser, function(req, res) {
db.authenticateUser(req.body, function(result) {
res.send(result);
});
});
/**
* API Handler to create a new user on registration
* Fails if a username already exists
* @param {[type]} req [Registration details - firstName, lastName, username, password, contactPicture]
* @return {[type]} [result object with flag indicating success or failure]
*/
app.post('/api/create', jsonParser, function(req, res) {
db.createUser(req.body, function(result) {
res.send(result);
});
});
/**
* API Handler to return all the users in the user_info table
* @param {[type]} req [userId requesting for the call]
* @return {[type]} [Return array of contact objects with details]
*/
app.post('/api/contacts', jsonParser, function(req, res) {
db.getContacts(req.body.userId, function(result) {
res.send(result);
});
});
/**
* API Handler to return all the conversations for the selected user
* @param {[type]} req [userId requesting for the call]
* @return {[type]} [Return array of conversation objects - having latest message, timestamp]
*/
app.post('/api/allConversations', jsonParser, function(req, res) {
db.getAllConversations(req.body.userId, function(result) {
res.send(result);
});
});
/**
* API Handler to return the messages for a particular conversation
* @param {[type]} req [userId requesting and the userId of the conversation]
* @return {[type]} [Return array of messages for the requested conversation]
*/
app.post('/api/conversation', jsonParser, function(req, res) {
db.getConversation(req.body.userId, req.body.otherUserId, function(result) {
res.send(result);
});
});
io.sockets.on('connection', function (socket) { // First connection
//Socket to handle and log, login of user
socket.on('login', function(userId) {
socket.userId = userId;
socket.loggedIn = true;
logger.info("[CONNECTED] userId", userId);
loggedInUsers[userId] = socket.id;
});
//Socket to handle and log, logout of user
socket.on('logout', function(userId) {
logger.info("[LOGOUT] userId", userId);
});
//Socket to handle messages being sent
socket.on('messageSent', function(message) {
db.addMessage(message.from, message.to, message.text, message.timestamp, message.image, message.blobURL || '', message.unread || 0);
if (loggedInUsers[message.to]) {
io.to(loggedInUsers[message.to]).emit('messageReceived', message);
}
});
//Socket to update the conversations to be read
socket.on('conversationRead', function(conversation) {
db.readConversation(conversation.from, conversation.to);
});
//Socket to update the connection termination
socket.on('disconnect', function () { // Disconnection of the client
if (socket.id && socket.loggedIn) {
socket.loggedIn = false;
logger.info("[DISCONNECTED] userId", socket.id);
}
delete loggedInUsers[socket.userId];
});
});
//Uncomment to enable opening window
opener('http://localhost:' + config.PORT);