-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketEvents.js
61 lines (50 loc) · 1.76 KB
/
socketEvents.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
function SocketEvents(requestToBridge){
this.requestToBridge = requestToBridge;
this.logger = require('nogg').logger('socketEvents');
};
SocketEvents.prototype = {
register: function(socket){
var self = this;
var requestToBridge = this.requestToBridge;
requestToBridge.execute(socket, 'user.connection', {}, function(err, result){
if(err){
self.logger.warn('bridge on event user.connection error');
self.logger.warn(err);
return;
}
});
socket.on('message', function(eventName, args, cb){
if(typeof eventName != "string"){
self.logger.warn('message without eventName received');
if(typeof cb == "function"){
cb('need eventName');
}
return;
}
if(typeof args != "object"){
args = {};
}
requestToBridge.execute(socket, eventName, args, function(err, result){
if(err){
self.logger.warn('bridge on event message error');
self.logger.warn(err);
}
if(typeof cb == "function"){
cb(err, result);
}
});
});
socket.on('disconnect', function(){
requestToBridge.execute(socket, 'user.disconnection', {}, function(err, result){
if(err){
self.logger.warn('bridge on event user.disconnection error');
self.logger.warn(err);
return;
}
});
});
}
};
module.exports = function(requestToBridge){
return new SocketEvents(requestToBridge);
};