-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
executable file
·305 lines (239 loc) · 8.96 KB
/
routes.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file is required by app.js. It sets up event listeners
// for the two main URL endpoints of the application - /create and /chat/:id
// and listens for socket.io messages.
// Use the gravatar module, to turn email addresses into avatar images:
var gravatar = require('gravatar');
var os = require('os');
var http = require('http');
// Export a function, so that we can pass
// the app and io instances from the app.js file:
module.exports = function(app,io){
app.get('/', function(req, res){
// Render views/home.html
res.render('home');
});
app.get('/videoChat', function(req, res){
// Render views/videoChat.html
res.render('videoChat');
});
app.get('/create', function(req,res){
// Generate unique id for the room
var id = Math.round((Math.random() * 1000000));
// Redirect to the random room
res.redirect('/chat/'+id);
});
app.get('/chat/:id', function(req,res){
// Render the chant.html view
res.render('chat');
});
// Initialize a new socket.io application, named 'chat'
var chat = io.on('connection', function (socket) {
// When the client emits the 'load' event, reply with the
// number of people in this chat room
socket.on('load',function(data){
var room = findClientsSocket(io,data);
if(room.length === 0 ) {
socket.emit('peopleinchat', {number: 0});
}
else if(room.length === 1) {
socket.emit('peopleinchat', {
number: 1,
user: room[0].username,
avatar: room[0].avatar,
id: data
});
}
else if (room.length >=2)
{
chat.emit('tooMany', {boolean: true});
}
});
// When the client emits 'login', save his name and avatar,
// and add them to the room
socket.on('login', function(data) {
var room = findClientsSocket(io, data.id);
// Only two people per room are allowed
if (room.length < 2) {
// Use the socket object to store data. Each client gets
// their own unique socket object
socket.username = data.user;
socket.room = data.id;
socket.email = data.email
socket.avatar = data.avatar;
//socket.avatar = gravatar.url(data.avatar, {s: '140', r: 'x', d: 'mm'});
// Tell the person what he should use for an avatar
socket.emit('img', socket.avatar);
// Add the client to the room
socket.join(data.id);
if (room.length == 1) {
var usernames = [],
avatars = [];
usernames.push(room[0].username);
usernames.push(socket.username);
avatars.push(room[0].avatar);
avatars.push(socket.avatar);
// Send the startChat event to all the people in the
// room, along with a list of people that are in it.
chat.in(data.id).emit('startChat', {
boolean: true,
id: data.id,
users: usernames,
avatars: avatars
});
}
}
else {
socket.emit('tooMany', {boolean: true});
}
});
// Somebody left the chat
socket.on('disconnect', function() {
// Notify the other person in the chat room
// that his partner has left
socket.broadcast.to(this.room).emit('leave', {
boolean: true,
room: this.room,
user: this.username,
avatar: this.avatar
});
// leave the room
socket.leave(socket.room);
});
socket.on('videoChat', function(data){
socket.emit('created', {senderId:data.senderId, user: this.username, avatar:this.avatar});
socket.broadcast.to(this.room).emit('created', {senderId: data.senderId, user: this.username,avatar: this.avatar});
socket.emit('videoChatInvite', {senderId: data.senderId, avatar: this.avatar});
socket.broadcast.to(this.room).emit('videoChatInvite', {senderId: data.senderId, avatar: this.avatar});
});
socket.on('audioChat', function(data){
socket.emit('audiocreated', {senderId:data.senderId, user: this.username, avatar:this.avatar});
socket.broadcast.to(this.room).emit('audiocreated', {senderId: data.senderId, user: this.username,avatar: this.avatar});
socket.emit('audioChatInvite', {senderId: data.senderId, avatar: this.avatar});
socket.broadcast.to(this.room).emit('audioChatInvite', {senderId: data.senderId, avatar: this.avatar});
});
socket.on('videoChatJoined', function(data){
socket.emit('joined',{senderId:data.senderId, user: this.username, avatar:this.avatar});
socket.broadcast.to(this.room).emit('joined', {senderId:data.senderId, user: this.username, avatar:this.avatar});
chat.in(data.senderId).emit('ready');
socket.broadcast.to(this.room).emit('ready');
});
socket.on('videoChatCancelled', function(data){
console.log("video call cancel");
chat.in(data.senderId).emit('videoChatSelfCancel', data.senderId);
socket.broadcast.to(this.room).emit('videoChatSelfCancel', data.senderId);
});
socket.on('videoChatRejected', function(data){
chat.in(data.senderId).emit('videoChatRefused', data.senderId);
socket.broadcast.to(this.room).emit('videoChatRefused', data.senderId);
});
socket.on('audioChatJoined', function(data){
socket.emit('audiojoined',{senderId:data.senderId, user: this.username, avatar:this.avatar});
socket.broadcast.to(this.room).emit('audiojoined', {senderId:data.senderId, user: this.username, avatar:this.avatar});
chat.in(data.senderId).emit('audioready');
socket.broadcast.to(this.room).emit('audioready');
});
socket.on('audioChatCancelled', function(data){
console.log("audio call cancel");
chat.in(data.senderId).emit('audioChatSelfCancel', data.senderId);
socket.broadcast.to(this.room).emit('audioChatSelfCancel', data.senderId);
});
socket.on('audioChatRejected', function(data){
chat.in(data.senderId).emit('audioChatRefused', data.senderId);
socket.broadcast.to(this.room).emit('audioChatRefused', data.senderId);
});
socket.on('emailChatHistory', function(data){
// When the server receives a message, it sends it to the other person in the room.
var webshot = require('node-webshot');
var options = {
shotSize: {
width: 'all'
, height: 'all'
}
, siteType:'html'
};
var chatHistoryImageName=Math.round((Math.random() * 1000000));
var recipientEmail = this.email;
webshot(data.emailtext, './public/chatScreenShot/chatHistory'+chatHistoryImageName+'.png', options, function(err) {
// screenshot now saved to hello_world.png
emailChatTranscript(recipientEmail,'./public/chatScreenShot/chatHistory'+chatHistoryImageName+'.png', 'chatHistory'+chatHistoryImageName+'.png');
});
});
// Handle the sending of messages
socket.on('msg', function(data){
// When the server receives a message, it sends it to the other person in the room.
socket.broadcast.to(socket.room).emit('receive', {isImage: data.isImage, msg: data.msg, user: data.user, img: data.img});
});
socket.on('snapReceived', function(data){
console.log(data);
chat.in(data.senderId).emit('renderSnap', {buf:data.buf, senderId:data.senderId, user:this.username, img:this.avatar});
socket.broadcast.to(this.room).emit('renderSnap', {buf:data.buf, senderId:data.senderId, user:this.username, img:this.avatar});
});
socket.on('message', function(data) {
// for a real app, would be room-only (not broadcast)
chat.in(data.senderId).emit('ice-message', data.message);
socket.broadcast.to(this.room).emit('ice-message', data.message);
});
socket.on('message-audio', function(data) {
// for a real app, would be room-only (not broadcast)
chat.in(data.senderId).emit('ice-message-audio', data.message);
socket.broadcast.to(this.room).emit('ice-message-audio', data.message);
});
socket.on('ipaddr', function() {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('ipaddr', details.address);
}
});
}
});
});
};
function emailChatTranscript(email, attachmentPath, attachmentName){
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'chatube531'
}
});
var msg = '<img src="cid:'+attachmentName+'" />'
var mailOptions = {
from: '[email protected]',
to: email,
subject: 'Chat history',
html: msg,
attachments: [{
filename: attachmentName,
path: attachmentPath,
cid: attachmentName //same cid value as in the html img src
}]
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
function findClientsSocket(io,roomId, namespace) {
var res = [],
ns = io.of(namespace ||"/"); // the default namespace is "/"
if (ns) {
for (var id in ns.connected) {
if(roomId) {
var index = ns.connected[id].rooms.indexOf(roomId) ;
if(index !== -1) {
res.push(ns.connected[id]);
}
}
else {
res.push(ns.connected[id]);
}
}
}
return res;
}