Skip to content

Commit

Permalink
Merge pull request socketio#569 from 3rd-Eden/blacklist
Browse files Browse the repository at this point in the history
Blacklist events
  • Loading branch information
rauchg committed Oct 11, 2011
2 parents 0339e74 + ecd20b0 commit 0224e4a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function Manager (server, options) {
, resource: '/socket.io'
, transports: defaultTransports
, authorization: false
, blacklist: ['disconnect']
, 'log level': 3
, 'close timeout': 25
, 'heartbeat timeout': 15
Expand Down
19 changes: 12 additions & 7 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ SocketNamespace.prototype.authorize = function (data, fn) {
SocketNamespace.prototype.handlePacket = function (sessid, packet) {
var socket = this.socket(sessid)
, dataAck = packet.ack == 'data'
, manager = this.manager
, self = this;

function ack () {
Expand Down Expand Up @@ -296,8 +297,7 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
if (packet.endpoint == '') {
connect();
} else {
var manager = this.manager
, handshakeData = manager.handshaken[sessid];
var handshakeData = manager.handshaken[sessid];

this.authorize(handshakeData, function (err, authorized, newData) {
if (err) return error(err);
Expand All @@ -322,13 +322,18 @@ SocketNamespace.prototype.handlePacket = function (sessid, packet) {
break;

case 'event':
var params = [packet.name].concat(packet.args);
// check if the emitted event is not blacklisted
if (-~manager.get('blacklist').indexOf(packet.name)) {
this.log.debug('ignoring blacklisted event `' + packet.name + '`');
} else {
var params = [packet.name].concat(packet.args);

if (dataAck) {
params.push(ack);
}
if (dataAck) {
params.push(ack);
}

socket.$emit.apply(socket, params);
socket.$emit.apply(socket, params);
}
break;

case 'disconnect':
Expand Down
39 changes: 39 additions & 0 deletions test/namespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,44 @@ module.exports = {
}
});
})
},

'ignoring blacklisted events': function (done) {
var cl = client(++ports)
, io = create(cl)
, calls = 0
, ws;

io.set('heartbeat interval', 1);
io.set('blacklist', ['foobar']);

io.sockets.on('connection', function (socket) {
socket.on('foobar', function () {
calls++;
});
});

cl.handshake(function (sid) {
ws = websocket(cl, sid);

ws.on('open', function (){
ws.packet({
type: 'event'
, name: 'foobar'
, endpoint: ''
});
});

ws.on('message', function (data) {
if (data.type === 'heartbeat') {
cl.end();
ws.finishClose();
io.server.close();

calls.should.equal(0);
done();
}
});
});
}
};

0 comments on commit 0224e4a

Please sign in to comment.