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

Skip to content

Commit ac8e859

Browse files
committed
support compression
1 parent 9ba6d47 commit ac8e859

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

Readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ server.listen(3000);
341341
});
342342
```
343343

344+
### Socket#compress(v:Boolean):Socket
345+
346+
Sets a modifier for a subsequent event emission that the event data will
347+
only be _compressed_ if the value is `true`. Defaults to `true` when you don't call the method.
348+
349+
```js
350+
var io = require('socket.io')();
351+
io.on('connection', function(socket){
352+
socket.compress(false).emit('an event', { some: 'data' });
353+
});
354+
```
355+
344356
### Client
345357

346358
The `Client` class represents an incoming transport (engine.io)

lib/client.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Client.prototype.setup = function(){
6161
Client.prototype.connect = function(name){
6262
debug('connecting to namespace %s', name);
6363
if (!this.server.nsps[name]) {
64-
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
64+
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'}, false, false, true);
6565
return;
6666
}
6767
var nsp = this.server.of(name);
@@ -135,17 +135,18 @@ Client.prototype.close = function(){
135135
* @param {Object} packet object
136136
* @param {Boolean} whether packet is already encoded
137137
* @param {Boolean} whether packet is volatile
138+
* @param {Boolean} whether packet should be compressed
138139
* @api private
139140
*/
140141

141-
Client.prototype.packet = function(packet, preEncoded, volatile){
142+
Client.prototype.packet = function(packet, preEncoded, volatile, compress){
142143
var self = this;
143144

144145
// this writes to the actual connection
145146
function writeToEngine(encodedPackets) {
146147
if (volatile && !self.conn.transport.writable) return;
147148
for (var i = 0; i < encodedPackets.length; i++) {
148-
self.conn.write(encodedPackets[i]);
149+
self.conn.write(encodedPackets[i], { compress: compress });
149150
}
150151
}
151152

lib/socket.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ Socket.prototype.write = function(){
202202
Socket.prototype.packet = function(packet, preEncoded){
203203
packet.nsp = this.nsp.name;
204204
var volatile = this.flags && this.flags.volatile;
205-
this.client.packet(packet, preEncoded, volatile);
205+
var compress = !this.flags || false !== this.flags.compress;
206+
this.client.packet(packet, preEncoded, volatile, compress);
206207
};
207208

208209
/**
@@ -442,3 +443,17 @@ Socket.prototype.disconnect = function(close){
442443
}
443444
return this;
444445
};
446+
447+
/**
448+
* Sets the compress flag.
449+
*
450+
* @param {Boolean} if `true`, compresses the sending data
451+
* @return {Socket} self
452+
* @api public
453+
*/
454+
455+
Socket.prototype.compress = function(compress){
456+
this.flags = this.flags || {};
457+
this.flags.compress = compress;
458+
return this;
459+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test": "mocha --reporter dot --slow 200ms --bail"
2020
},
2121
"dependencies": {
22-
"engine.io": "1.5.1",
22+
"engine.io": "Automattic/engine.io",
2323
"socket.io-parser": "2.2.2",
2424
"socket.io-client": "1.3.2",
2525
"socket.io-adapter": "0.3.1",

test/socket.io.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,36 @@ describe('socket.io', function(){
13291329
});
13301330
});
13311331
});
1332+
1333+
it('should enable compression by default', function(done){
1334+
var srv = http();
1335+
var sio = io(srv);
1336+
srv.listen(function(){
1337+
var socket = client(srv);
1338+
sio.on('connection', function(s){
1339+
s.conn.once('packetCreate', function(packet) {
1340+
expect(packet.options.compress).to.be(true);
1341+
done();
1342+
});
1343+
s.emit('woot', 'hi');
1344+
});
1345+
});
1346+
});
1347+
1348+
it('should disable compression', function(done){
1349+
var srv = http();
1350+
var sio = io(srv);
1351+
srv.listen(function(){
1352+
var socket = client(srv);
1353+
sio.on('connection', function(s){
1354+
s.conn.once('packetCreate', function(packet) {
1355+
expect(packet.options.compress).to.be(false);
1356+
done();
1357+
});
1358+
s.compress(false).emit('woot', 'hi');
1359+
});
1360+
});
1361+
});
13321362
});
13331363

13341364
describe('messaging many', function(){

0 commit comments

Comments
 (0)