Skip to content

Commit

Permalink
[fix] Stop brodcasting from a destroyed spark
Browse files Browse the repository at this point in the history
  • Loading branch information
cayasso committed Jan 2, 2014
1 parent 124a02e commit b4ed030
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
18 changes: 12 additions & 6 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ <h1>Welcome to Primus Rooms example.</h2>

var primus = new Primus.connect('ws://localhost:8080');

primus.on('open', function () {
primus.on('data', function (msg) {
console.log('MESSAGE ===>', msg);
});

/*primus.on('open', function () {
console.log('OPENED');
// Send request to join the news room
primus.write(room);
Expand All @@ -29,25 +35,25 @@ <h1>Welcome to Primus Rooms example.</h2>
// on data received
primus.on('data', function (data) {
console.log('MSG:', data);
});
});*/
}

// Set one more client
client('room1');


// Set one more client
client('room2');
//client('room2');


// Set one more client
client('room3');
//client('room3');

// Set one more client
client('room4');
//client('room4');

// special client
client('me');
//client('me');


</script>
Expand Down
35 changes: 31 additions & 4 deletions examples/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,40 @@ var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });


// add rooms to Primus
primus.use('rooms', rooms);
//primus.use('rooms', rooms);
//primus.use('emitter', 'primus-emitter');

var timer = null;

primus.on('connection', function (spark) {

spark.on('data', function (room) {


//spark.join('hola1');
/*spark.join('hola2');
spark.join('hola3');
spark.join('hola4');
spark.join('hola5');
spark.join('hola6');
spark.join('hola7');
spark.join('hola8');
spark.join('hola9');
spark.join('hola10');
spark.join('hola11');
spark.join('hola12');
spark.join('hola13');
spark.join('hola14');
spark.join('hola15');*/


setInterval(function () {
spark.write('DATA IN THE HOUSE ' + spark.id);
//spark.room('hola1').write('hola');
}, 5000);



/*spark.on('data', function (room) {
if ('me' === room) {
Expand Down Expand Up @@ -54,12 +81,12 @@ primus.on('connection', function (spark) {
// leaving room room2
spark.leave('room2');
});
});*/

});


// Start server listening
server.listen(process.env.PORT || 8080, function(){
console.log('\033[96mlistening on localhost:8081 \033[39m');
console.log('\033[96mlistening on localhost:8080 \033[39m');
});
14 changes: 12 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ PrimusRooms.Spark = function RoomsSpark(Spark) {

Spark.prototype.initialise = function initialise() {
this._rooms = [];
this.once('end', this.leaveAll);
this.once('end', this.leaveAll.bind(this));
init.apply(this, arguments);
};

Expand All @@ -204,7 +204,17 @@ PrimusRooms.Spark = function RoomsSpark(Spark) {

Spark.prototype.write = function writing(data) {
var sparks = this.primus.connections;
return this.primus._rooms.broadcast(this, [data], sparks, [this.id], 'write') ?

// we need to make sure this is a valid spark
// and not a destroyed one, destroyed sparks are
// no longer part of the connection list.
var spark = sparks[this.id];

// if this is not a valid spark we just ignore the
// write request
if (!spark) return;

return this.primus._rooms.broadcast(spark, [data], sparks, [spark.id], 'write') ?
true : write.call(this, data);
};

Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,32 @@ describe('primus-rooms', function () {
});
});

it('should not allow broadcasting from a destroyed spark', function (done) {
srv.listen(function () {

primus.on('connection', function (spark) {
spark.join('room1');
});

primus.on('disconnection', function (spark) {
spark.room('room1').write('hola');
done();
});

var c1 = client(srv, primus);

c1.on('open', function () {
var c2 = client(srv, primus);
c2.on('open', c2.end);
});

c1.on('data', function () {
done(new Error('not'));
});

});
});

it('should get all clients connected to a room', function (done) {
var ids = [];
srv.listen(function () {
Expand Down

0 comments on commit b4ed030

Please sign in to comment.