Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

transactor handles closing sockets

  • Loading branch information...
commit d04a36afdb8c52ef6c302ee27b0d3c947ad80e92 1 parent ddcae88
ethersheet-collective authored
Showing with 30 additions and 7 deletions.
  1. +1 −1  package.json
  2. +20 −0 test/transactor.js
  3. +9 −6 transactor.js
View
2  package.json
@@ -10,7 +10,7 @@
"url": "git://github.com/ethersheet-collective/transactor.git"
},
"dependencies": {
- "socket.io": ">=0.9.5"
+ "node-uuid": "*"
},
"devDependencies": {
"mocha": ">=1.3.0",
View
20 test/transactor.js
@@ -85,5 +85,25 @@ describe('Transactor', function(){
setTimeout(done,20);
});
});
+
+ describe('when a socket closes', function(){
+ beforeEach(function(){
+ trans.onTransaction(function(channel,data,cb){
+ cb(null,data);
+ });
+ });
+
+ it('should be removed from the sockets array', function(done){
+ var socket1 = new EventEmitter();
+ socket1.write = function(data){
+ throw new Error('should not be called');
+ };
+ trans.addSocket('channel_1',socket1);
+ socket1.emit('close');
+ socket.emit('data', test_data);
+ setTimeout(done,20);
+ });
+
+ });
});
});
View
15 transactor.js
@@ -1,3 +1,5 @@
+var uuid = require('node-uuid');
+
var Transactor = function(o){
o = o || {};
if(typeof o.transaction_handler === 'function'){
@@ -14,10 +16,10 @@ Transactor.prototype.onTransaction = function(transaction_handler){
Transactor.prototype.addSocket = function(channel,socket){
var trans = this;
-
+ var socket_id = uuid.v4();
// add socket to socket pool
- if(!this.sockets[channel]) this.sockets[channel] = [];
- this.sockets[channel].push(socket);
+ if(!this.sockets[channel]) this.sockets[channel] = {};
+ this.sockets[channel][socket_id] = socket;
// add the supplied transaction handler to each channel on the socket
socket.on('data',function(data){
@@ -29,13 +31,14 @@ Transactor.prototype.addSocket = function(channel,socket){
// add the supplied disconnection handler
socket.on('close', function(){
+ delete trans.sockets[channel][socket_id];
});
};
Transactor.prototype.broadcast = function(channel,data){
if(!this.sockets[channel]) return;
- this.sockets[channel].forEach(function(socket){
- socket.write(data);
- });
+ for( var socket_id in this.sockets[channel] ){
+ this.sockets[channel][socket_id].write(data);
+ }
};
Please sign in to comment.
Something went wrong with that request. Please try again.