-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketEvents.js
More file actions
89 lines (74 loc) · 2.29 KB
/
socketEvents.js
File metadata and controls
89 lines (74 loc) · 2.29 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
var jwt = require('jsonwebtoken');
var config = require('./config');
var User = require('./backend/models/user');
class Client{
constructor(socket, userId){
this.socket = socket;
this.userId = userId;
this.updateUserId = this.updateUserId.bind(this);
}
updateUserId(userId){
this.userId = userId;
}
getSocketId(){
return this.socket.id;
}
getUserId(){
return this.userId;
}
}
var clients = {};
exports = module.exports = function(io) {
// Set socket.io listeners.
io.on('connection', (socket) => {
console.log('a user connected');
var client = new Client(socket, null);
// Receive user's token, decode, store key-value pair
socket.on('user token', (token) => {
console.log("Receive token: " + token);
jwt.verify(token, config.secret, function(err, decoded) { // Decode user token to obtain user id
if (err) {
// TODO: inform user that the token cannot be decoded
console.log(err);
return;
}
var userId = decoded.id;
client.updateUserId(userId);
clients[userId] = client;
});
})
// Receive 'new message', need to push notification
socket.on('new message', (conversation, recipient) => {
console.log("Receive new message");
// If the user is online, push notification
if (clients[recipient]){
socket.broadcast.to(clients[recipient].getSocketId()).emit('refresh messages', conversation);
return;
}
// If the user is offline, update the user data
User.findById(recipient)
.then((doc) => {
var conv = doc.conversations;
conv.push(conversation);
doc.set({
"conversations" : conv,
})
doc.save();
})
.catch((err) => console.log(err));
});
socket.on('disconnect', () => {
console.log('user disconnected');
delete clients[client.getUserId];
});
});
}
// On conversation entry, join broadcast channel
// socket.on('enter conversation', (conversation) => {
// socket.join(conversation);
// console.log('joined ' + conversation);
// });
// socket.on('leave conversation', (conversation) => {
// socket.leave(conversation);
// console.log('left ' + conversation);
// })