Skip to content

Add the "loopback" flag. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ Client.prototype.packet = function(packet, opts){
opts = opts || {};
var self = this;

// handle loopback packets as though they came in off the wire.
if (opts.loopback) {
if (opts.preEncoded) { // a broadcast pre-encodes a packet
for (var i = 0; i < packet.length; i++) {
this.decoder.add(packet[i]);
}
} else {
this.ondecoded(packet);
}
return;
}

// this writes to the actual connection
function writeToEngine(encodedPackets) {
if (opts.volatile && !self.conn.transport.writable) return;
Expand Down
1 change: 1 addition & 0 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports.events = [
*/

exports.flags = [
'loopback',
'json',
'volatile',
'local'
Expand Down
7 changes: 6 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exports.events = [
*/

var flags = [
'loopback',
'json',
'volatile',
'broadcast',
Expand Down Expand Up @@ -155,6 +156,10 @@ Socket.prototype.emit = function(ev){
throw new Error('Callbacks are not supported when broadcasting');
}

if (this.flags.loopback) {
throw new Error('Callbacks are not supported when emitting loopback messages');
}

debug('emitting packet with ack id %d', this.nsp.ids);
this.acks[this.nsp.ids] = args.pop();
packet.id = this.nsp.ids++;
Expand Down Expand Up @@ -311,7 +316,7 @@ Socket.prototype.onconnect = function(){
};

/**
* Called with each packet. Called by `Client`.
* Called with each packet. Called by `Client` and `emit` with `loopback` flag.
*
* @param {Object} packet
* @api private
Expand Down
33 changes: 31 additions & 2 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ describe('socket.io', function(){
});
});
});

it('should not reuse same-namespace connections', function(done){
var srv = http();
var sio = io(srv);
Expand Down Expand Up @@ -837,6 +837,35 @@ describe('socket.io', function(){
}, 500);
});

it('should emit loopback messages locally', function(done) {
var srv = http();
var sio = io(srv);

var local_counter = 0;
var remote_counter = 0;
srv.listen(function(){
sio.of('/chat').on('connection', function(s){
setTimeout(function() {
sio.of('/chat').loopback.emit('ev', 'data');
}, 50);
s.on('ev', function() {
local_counter++;
});
});

var socket = client(srv, '/chat');
socket.on('ev', function() {
remote_counter++;
});
});

setTimeout(function() {
expect(local_counter).to.be(1);
expect(remote_counter).to.be(0);
done();
}, 500);
});

it('should emit volatile event', function(done) {
var srv = http();
var sio = io(srv);
Expand Down Expand Up @@ -1608,7 +1637,7 @@ describe('socket.io', function(){
});
});
});

it('should see query parameters sent from secondary namespace connections in handshake object', function(done){
var srv = http();
var sio = io(srv);
Expand Down