Skip to content

Commit 9afd5f2

Browse files
committed
1.0.0
1 parent b809b05 commit 9afd5f2

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

modules/lib/message.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
const _ = require('lodash');
12

23
exports.broadcast = function(socket,data){
34

4-
55
security.verifyConnection(socket,data).then(function(result) {
66

77
console.log('Access allowed to broadcast()');
88
try{
99
var obj = {org:data.org,topic:data.topic,message:data.message};
1010
console.log('socket.io broadcast:',obj);
11-
clients.forEach(function(client) {
11+
_.each(clients,function(client,index) {
12+
1213
client.emit('message', obj);
1314
}
1415
);

modules/lib/security.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@ const _ = require('lodash'),
44

55

66
const func = (socket,data) => {
7+
78
return new Promise((resolve, reject) => {
89

9-
verifyConnection(socket,data);
10-
});
11-
};
12-
13-
exports.verifyConnection = func;
14-
15-
const verifyConnection = function(socket, data) {
16-
17-
var token = data && data.token || null;
10+
var token = data && data.token || null;
1811

1912
if(!socket || socket==='undefined' || !clients[socket.id] || !token)
2013
{
@@ -54,30 +47,23 @@ const verifyConnection = function(socket, data) {
5447
}
5548
else {
5649
console.log('user:true');
57-
resolve(true);}
50+
resolve(true);
51+
}
5852

5953
});
6054
}
61-
62-
};
63-
64-
65-
const func2 = (socket,data) => {
66-
return new Promise((resolve, reject) => {
67-
68-
verifyConnectionAuth0(socket,data);
69-
7055
});
7156
};
7257

58+
exports.verifyConnection = func;
7359

7460

75-
exports.verifyConnectionAuth0 = func2;
7661

77-
exports.verifyConnectionAuth0 = function(socket, data) {
7862

79-
80-
var token = data && data.token || null;
63+
const func2 = (socket,data) => {
64+
return new Promise((resolve, reject) => {
65+
66+
var token = data && data.token || null;
8167

8268

8369
if(!socket || socket==='undefined' || !clients[socket.id] || !token)
@@ -123,9 +109,16 @@ exports.verifyConnectionAuth0 = function(socket, data) {
123109

124110
});
125111
};
126-
112+
113+
});
127114
};
128115

116+
117+
118+
exports.verifyConnectionAuth0 = func2;
119+
120+
121+
129122
exports.authenticateUser = function authenticateUser (socket, data) {
130123

131124
const {email,password}=data;
@@ -165,7 +158,7 @@ const loginUser = function loginUser(socket, user) {
165158
var data = {profile: profile, token:token};
166159
socket.emit('login.success', data);
167160

168-
clients[socket.id]=data;
161+
clients[socket.id]=socket;
169162
//clients[token]=data;
170163
console.log('User logged to socket.io. number of authorized connections:',NumberOfConnections());
171164
console.log('User profile logged to socket.io=',profile);

modules/mainSocketLocalToken.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
21
/*
32
Socket.io Authentication based on Local generated token
43
*/
5-
64
const user = require('./lib/user'),
75
socketio = require('socket.io');
86

@@ -18,8 +16,6 @@ global.NumberOfConnections = function() {
1816
return Object.keys(clients).length;
1917
}
2018

21-
22-
2319
exports.connect = function(server){
2420

2521
var io = socketio.listen(server);
@@ -54,14 +50,11 @@ exports.connect = function(server){
5450

5551
});
5652

57-
58-
59-
socket.on('disconnect', function () {
53+
54+
socket.on('disconnect', function () {
6055
clients[socket.id] && delete clients[socket.id];
6156
console.log('close connection, number of authorized connections:',NumberOfConnections());
62-
63-
64-
});
57+
});
6558
});
6659

6760
io.on('end', function() {

0 commit comments

Comments
 (0)