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

Skip to content

Commit a707284

Browse files
committed
Merge pull request socketio#2006 from nkzawa/patch-4
improve Socket#packet and Client#packet and enable volatile and compress when broadcast
2 parents 5826ef0 + 81aea99 commit a707284

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

lib/client.js

Lines changed: 7 additions & 8 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'}, false, false, true);
64+
this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'});
6565
return;
6666
}
6767
var nsp = this.server.of(name);
@@ -133,26 +133,25 @@ Client.prototype.close = function(){
133133
* Writes a packet to the transport.
134134
*
135135
* @param {Object} packet object
136-
* @param {Boolean} whether packet is already encoded
137-
* @param {Boolean} whether packet is volatile
138-
* @param {Boolean} whether packet should be compressed
136+
* @param {Object} options
139137
* @api private
140138
*/
141139

142-
Client.prototype.packet = function(packet, preEncoded, volatile, compress){
140+
Client.prototype.packet = function(packet, opts){
141+
opts = opts || {};
143142
var self = this;
144143

145144
// this writes to the actual connection
146145
function writeToEngine(encodedPackets) {
147-
if (volatile && !self.conn.transport.writable) return;
146+
if (opts.volatile && !self.conn.transport.writable) return;
148147
for (var i = 0; i < encodedPackets.length; i++) {
149-
self.conn.write(encodedPackets[i], { compress: compress });
148+
self.conn.write(encodedPackets[i], { compress: opts.compress });
150149
}
151150
}
152151

153152
if ('open' == this.conn.readyState) {
154153
debug('writing packet %j', packet);
155-
if(!preEncoded) { // not broadcasting, need to encode
154+
if (!opts.preEncoded) { // not broadcasting, need to encode
156155
this.encoder.encode(packet, function (encodedPackets) { // encode, then write results to engine
157156
writeToEngine(encodedPackets);
158157
});

lib/socket.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ Socket.prototype.emit = function(ev){
132132
var packet = {};
133133
packet.type = hasBin(args) ? parser.BINARY_EVENT : parser.EVENT;
134134
packet.data = args;
135+
var flags = this.flags || {};
135136

136137
// access last argument to see if it's an ACK callback
137138
if ('function' == typeof args[args.length - 1]) {
138-
if (this._rooms || (this.flags && this.flags.broadcast)) {
139+
if (this._rooms || flags.broadcast) {
139140
throw new Error('Callbacks are not supported when broadcasting');
140141
}
141142

@@ -144,15 +145,18 @@ Socket.prototype.emit = function(ev){
144145
packet.id = this.nsp.ids++;
145146
}
146147

147-
if (this._rooms || (this.flags && this.flags.broadcast)) {
148+
if (this._rooms || flags.broadcast) {
148149
this.adapter.broadcast(packet, {
149150
except: [this.id],
150151
rooms: this._rooms,
151-
flags: this.flags
152+
flags: flags
152153
});
153154
} else {
154155
// dispatch packet
155-
this.packet(packet);
156+
this.packet(packet, {
157+
volatile: flags.volatile,
158+
compress: flags.compress
159+
});
156160
}
157161

158162
// reset flags
@@ -196,14 +200,15 @@ Socket.prototype.write = function(){
196200
* Writes a packet.
197201
*
198202
* @param {Object} packet object
203+
* @param {Object} options
199204
* @api private
200205
*/
201206

202-
Socket.prototype.packet = function(packet, preEncoded){
207+
Socket.prototype.packet = function(packet, opts){
203208
packet.nsp = this.nsp.name;
204-
var volatile = this.flags && this.flags.volatile;
205-
var compress = !this.flags || false !== this.flags.compress;
206-
this.client.packet(packet, preEncoded, volatile, compress);
209+
opts = opts || {};
210+
opts.compress = false !== opts.compress;
211+
this.client.packet(packet, opts);
207212
};
208213

209214
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"engine.io": "automattic/engine.io#ddc64a",
2323
"socket.io-parser": "2.2.3",
2424
"socket.io-client": "automattic/socket.io-client#210d65",
25-
"socket.io-adapter": "automattic/socket.io-adapter#ae79d8",
25+
"socket.io-adapter": "nkzawa/socket.io-adapter#bd48c2f292",
2626
"has-binary": "0.1.6",
2727
"debug": "2.1.3"
2828
},

0 commit comments

Comments
 (0)