-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
149 lines (106 loc) · 3.8 KB
/
Copy pathserver.js
File metadata and controls
149 lines (106 loc) · 3.8 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
//imports
const jwt = require('jsonwebtoken');
const server = require('http').createServer();
const io = require('socket.io')(server);
const map = require('hashmap');
const bluebird = require("bluebird");
//Redis
const redis = require("redis");
/**********************************/
/******** Socket MAP *******/
/**********************************/
var usersSoketsId = new map();
/**********************************/
/**** fucntion handler *****/
/**********************************/
function handler(req, res) {
res.writeHead(200);
res.end("ok");
}
// app key Configs
var envJWTSecret = "APP KEY JWT FROM LARAVEL ENV";
// Port SocketServer
var port = 3000;
console.log("APPKEY: " + envJWTSecret )
/**********************************/
/******** REDIS ***********/
/**********************************/
var redisClient = redis.createClient();
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
redisClient.subscribe("message");
/**********************************/
/******** SOCKETS TOKEN ***********/
/**********************************/
io.use(function(socket, next) {
var decoded = null;
try {
decoded = jwt.verify(socket.handshake.query.jwt, envJWTSecret);
} catch (err) {
console.error(err);
next(new Error('Invalid token!'));
}
if (decoded) {
// everything went fine - save userId as property of given connection instance
socket.usuario = decoded.user // Save user in socket obj
return next();
} else {
// invalid token - terminate the connection
socket.conn.close();
next(new Error('Invalid token!'));
}
});
/**********************************/
/***** ON CONNECTION EVENT ***/
/**********************************/
io.on('connection', function(socket) {
//socket.join('usuario.' + socket.usuario);
console.log("User: " + socket.user + " connected with socket.id " + socket.id);
//Push socket id and user in usermap
usersSoketsId.set(socket.user,socket.id);
console.log("added user to mapUsers: " + usersSoketsId.get(socket.user));
/**********************************/
/********* Disconnect *********/
/**********************************/
socket.on('disconnect', function () {
let toRemove = socket.user;
console.log("user: " + toRemove +" with socket.id " + socket.id +" is disconnect");
//delete socket id and user in usermap
try{
usersSoketsId.delete(toRemove);
console.log("user was removed with socket id " + socket.id);
}
catch{
console.log("error to try delete user in the userMap")
}
socket.conn.close();
});
});
/**********************************/
/******* LARAVEL EVENT *********/
/**********************************/
redisClient.on('message',function(channel, data){
let eventNotPopUp;
//Obj to JSON
eventNotPopUp = JSON.parse(data);
//Extraemos Usuario del evento
let userOfEvent = data.usuario;
console.log("Event recived from laravel " + channel + " to user " + userOfEvent );
let sId = usersSoketsId.get(userOfEvent);
//User is connected
if(sId)
{
console.log("Notification to: " + userOfEvent);
// sending to individual socketid (private message)
io.sockets.connected[sId].emit('newNotification', eventNotPopUp ,function (callback) {
console.log("messege arrived");
eventNotPopUp.delivered = true;
});
}
//User is not connected
else
{
console.log("User is not connected");
eventNotPopUp.delivered = false;
}
});