-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
183 lines (147 loc) · 5.58 KB
/
app.js
File metadata and controls
183 lines (147 loc) · 5.58 KB
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
//This is the main file of this app.Here server side coding takes place
//Start this application by running 'node app.js' from the terminal
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
mongoose = require('mongoose') ;
//cache some objects
var session = [],
name = [],
flag= 0;
//for stopwatch
var time= 0,
timer=0,
running = 0;
// configuration of this app
app.set('views', __dirname+ '/views');
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// configuration for mongodb
mongoose.connect('mongodb://localhost/chat', function(err){
if(err){
console.log(err);
} else{
console.log('Connected to mongodb!');
// create a schema
var Schema = mongoose.Schema;
var chatSchema = new Schema({
user : String,
msg : String,
ses : String,
created : {type: Date, default: Date.now}
});
var Chat = mongoose.model('Message', chatSchema);
//routing
app.get('/', function(req,res){
res.render('home');
});
//a new socket.io application
io.sockets.on('connection', function(socket){
//when the user emits login from the client side
//save his name and do other staffs
socket.on('login',function(data, callback){
// Use the socket object to store data.
socket.username= data;
name.push(socket.username);
if(session.length > 0){
callback(false);
if(flag){
updateUserNames();
}
}else{
callback(true);
Chat.distinct('ses',{user:socket.username}, function(err , docs){
if(err) throw err;
socket.emit('load all sessions', docs);
});
}
if(running){
time = timer;
increment(time);
}
socket.emit('session', session[0]);
});
//when the user creates a session
//save this information
socket.on('session',function(data){
flag = 1;
timer = 9000;
// Use the socket object to store data.
socket.sessionname= data;
session.push(socket.sessionname);
socket.emit('session', socket.sessionname);
updateUserNames();
running=1;
time = timer;
setInterval(function(){
timer--;
},100);
increment(time);
});
// Handle the sending of messages
socket.on('msg', function(data){
var msg = data.msg.trim();
var newMsg = new Chat ({user: socket.username, ses: session[0],
msg: msg });
newMsg.save(function(err){
if(err) throw err;
//When the server receives a message,
//it sends it to the other person in the session.
socket.broadcast.emit('receive', {msg: data.msg, user: data.user});
});
});
//when an user turns off the session
socket.on('Stop Session', function(){
updateTimer();
});
//when an user want to his previous activities
socket.on('load all messages', function(data){
Chat.find({ses: data}, function(err, docs){
if(err) throw err;
socket.emit('load all messages', docs);
});
});
// for countdown for the session
function increment(time){
var myVar = setInterval(function(){
if(running == 1 && time <= 9000 && time > 0) {
time--;
var min = Math.floor(time / 10 / 60);
var secs = Math.floor(time / 10);
socket.emit('timer', {min: min, secs: secs});
}else{
stop();
}
},100);
function stop(){
clearInterval(myVar);
updateTimer();
}
}
function updateUserNames(){
socket.emit('user', name);
socket.broadcast.emit('user', name);
}
function updateTimer(){
running = 0;
name = [];
session = [];
timer = 9000;
socket.emit('Session Ended');
}
//when the user disconnected
socket.on('disconnect', function(data){
if(! socket.username) return;
name.splice(name.indexOf(socket.username), 1);
updateUserNames();
});
});
}
});
//server will listen to this port
server.listen(process.env.PORT || 3000, process.env.IP || "127.0.0.1", function(){
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});