Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53cdd8f

Browse files
committed
support flags on namespace
1 parent a93d05a commit 53cdd8f

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,17 @@ Server.prototype.close = function(){
361361
* Expose main namespace (/).
362362
*/
363363

364-
['on', 'to', 'in', 'use', 'emit', 'send', 'write', 'clients'].forEach(function(fn){
364+
['on', 'to', 'in', 'use', 'emit', 'send', 'write', 'clients', 'compress'].forEach(function(fn){
365365
Server.prototype[fn] = function(){
366366
var nsp = this.sockets[fn];
367367
return nsp.apply(this.sockets, arguments);
368368
};
369369
});
370370

371371
Namespace.flags.forEach(function(flag){
372-
Server.prototype.__defineGetter__(flag, function(name){
373-
this.flags.push(name);
372+
Server.prototype.__defineGetter__(flag, function(){
373+
this.sockets.flags = this.sockets.flags || {};
374+
this.sockets.flags[flag] = true;
374375
return this;
375376
});
376377
});

lib/namespace.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ exports.events = [
2929
* Flags.
3030
*/
3131

32-
exports.flags = ['json'];
32+
exports.flags = [
33+
'json',
34+
'volatile'
35+
];
3336

3437
/**
3538
* `EventEmitter#emit` reference.
@@ -252,3 +255,17 @@ Namespace.prototype.clients = function(fn){
252255
this.adapter.clients(this.rooms, fn);
253256
return this;
254257
};
258+
259+
/**
260+
* Sets the compress flag.
261+
*
262+
* @param {Boolean} if `true`, compresses the sending data
263+
* @return {Socket} self
264+
* @api public
265+
*/
266+
267+
Namespace.prototype.compress = function(compress){
268+
this.flags = this.flags || {};
269+
this.flags.compress = compress;
270+
return this;
271+
};

test/socket.io.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ describe('socket.io', function(){
405405
expect(sio.emit).to.be.a('function');
406406
expect(sio.send).to.be.a('function');
407407
expect(sio.write).to.be.a('function');
408+
expect(sio.clients).to.be.a('function');
409+
expect(sio.compress).to.be.a('function');
410+
expect(sio.json).to.be(sio);
411+
expect(sio.volatile).to.be(sio);
412+
expect(sio.sockets.flags).to.eql({ json: true, volatile: true });
413+
delete sio.sockets.flags;
408414
});
409415

410416
it('should automatically connect', function(done){
@@ -730,6 +736,87 @@ describe('socket.io', function(){
730736
});
731737
}
732738
});
739+
740+
it('should not emit volatile event after regular event', function(done) {
741+
var srv = http();
742+
var sio = io(srv);
743+
744+
var counter = 0;
745+
srv.listen(function(){
746+
sio.of('/chat').on('connection', function(s){
747+
// Wait to make sure there are no packets being sent for opening the connection
748+
setTimeout(function() {
749+
sio.of('/chat').emit('ev', 'data');
750+
sio.of('/chat').volatile.emit('ev', 'data');
751+
}, 20);
752+
});
753+
754+
var socket = client(srv, '/chat');
755+
socket.on('ev', function() {
756+
counter++;
757+
});
758+
});
759+
760+
setTimeout(function() {
761+
expect(counter).to.be(1);
762+
done();
763+
}, 200);
764+
});
765+
766+
it('should emit volatile event', function(done) {
767+
var srv = http();
768+
var sio = io(srv);
769+
770+
var counter = 0;
771+
srv.listen(function(){
772+
sio.of('/chat').on('connection', function(s){
773+
// Wait to make sure there are no packets being sent for opening the connection
774+
setTimeout(function() {
775+
sio.of('/chat').volatile.emit('ev', 'data');
776+
}, 20);
777+
});
778+
779+
var socket = client(srv, '/chat');
780+
socket.on('ev', function() {
781+
counter++;
782+
});
783+
});
784+
785+
setTimeout(function() {
786+
expect(counter).to.be(1);
787+
done();
788+
}, 200);
789+
});
790+
791+
it('should enable compression by default', function(done){
792+
var srv = http();
793+
var sio = io(srv);
794+
srv.listen(function(){
795+
var socket = client(srv, '/chat');
796+
sio.of('/chat').on('connection', function(s){
797+
s.conn.once('packetCreate', function(packet) {
798+
expect(packet.options.compress).to.be(true);
799+
done();
800+
});
801+
sio.of('/chat').emit('woot', 'hi');
802+
});
803+
});
804+
});
805+
806+
it('should disable compression', function(done){
807+
var srv = http();
808+
var sio = io(srv);
809+
srv.listen(function(){
810+
var socket = client(srv, '/chat');
811+
sio.of('/chat').on('connection', function(s){
812+
s.conn.once('packetCreate', function(packet) {
813+
expect(packet.options.compress).to.be(false);
814+
done();
815+
});
816+
sio.of('/chat').compress(false).emit('woot', 'hi');
817+
});
818+
});
819+
});
733820
});
734821

735822
describe('socket', function(){

0 commit comments

Comments
 (0)