Skip to content

Commit

Permalink
better message count logic
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Mar 7, 2013
1 parent a9faaf8 commit efb26e7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 103 deletions.
7 changes: 3 additions & 4 deletions examples/clients/web/actionHeroWebSocket.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@


actionHeroWebSocket.prototype.handleMessage = function(message){ actionHeroWebSocket.prototype.handleMessage = function(message){
var self = this; var self = this;

if(message.context == "response"){ if(message.context == "response"){
if(typeof self.callbacks[self.messageCount] === 'function'){ if(typeof self.callbacks[message.messageCount] === 'function'){
self.callbacks[self.messageCount](message); self.callbacks[message.messageCount](message);
} }
delete self.callbacks[self.messageCount]; delete self.callbacks[message.messageCount];
} }


else if(message.context == "user"){ else if(message.context == "user"){
Expand Down
2 changes: 2 additions & 0 deletions initializers/webSocketServer.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ var webSocketServer = function(api, next){
if(connection.response.context == "response"){ if(connection.response.context == "response"){
if(proxy_connection.respondingTo != null){ if(proxy_connection.respondingTo != null){
connection.response.messageCount = proxy_connection.respondingTo; connection.response.messageCount = proxy_connection.respondingTo;
}else if(proxy_connection.messageCount != null){
connection.response.messageCount = proxy_connection.messageCount;
}else{ }else{
connection.response.messageCount = connection.messageCount; connection.response.messageCount = connection.messageCount;
} }
Expand Down
174 changes: 75 additions & 99 deletions test/client_webSockets.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ describe('Client: Web Sockets', function(){
var client_2 = new actionHeroWebSocket({host: socketURL, faye: faye}); var client_2 = new actionHeroWebSocket({host: socketURL, faye: faye});
var client_3 = new actionHeroWebSocket({host: socketURL, faye: faye}); var client_3 = new actionHeroWebSocket({host: socketURL, faye: faye});


function makeSocketRequest(thisClient, type, data, cb){
var listener = function(response){
thisClient.removeListener('response', listener);
cb(response);
};
thisClient.on('response', listener);
thisClient.emit(type, data);
}

function countWebSocketConnections(){ function countWebSocketConnections(){
var found = 0; var found = 0;
for(var i in apiObj.connections.connections){ for(var i in apiObj.connections.connections){
Expand Down Expand Up @@ -46,7 +37,7 @@ describe('Client: Web Sockets', function(){


setTimeout(function(){ setTimeout(function(){
apiObj.faye.client.publish("/test", {message: 'hello'}); apiObj.faye.client.publish("/test", {message: 'hello'});
}, 1000); }, 500);
}); });


it('socket client connections should work: client 1', function(done){ it('socket client connections should work: client 1', function(done){
Expand All @@ -59,103 +50,88 @@ describe('Client: Web Sockets', function(){
}); });
}); });


// it('socket client connections should work: client 2', function(done){ it('socket client connections should work: client 2', function(done){
// client_2 = io.connect(socketURL, io_options); client_2.connect(function(err, data){
// client_2.on('welcome', function(data){ data.should.be.an.instanceOf(Object);
// data.should.be.an.instanceOf(Object); data.context.should.equal("response");
// data.context.should.equal("api"); data.details.room.should.equal("defaultRoom");
// data.room.should.equal("defaultRoom"); data.details.totalActions.should.equal(0);
// setTimeout(function(){ done();
// done(); });
// }, 1000); });
// });
// });


// it('Other clients should have been told about people entering the room', function(done){ it('Other clients should have been told about people entering the room', function(done){
// this.timeout(3000) client_3.connect(function(err, data){
// var listener = function(response){ data.should.be.an.instanceOf(Object);
// client_1.removeListener('say', listener); data.context.should.equal("response");
// response.should.be.an.instanceOf(Object); data.details.room.should.equal("defaultRoom");
// response.context.should.equal('user'); data.details.totalActions.should.equal(0);
// response.message.should.equal('I have entered the room'); done();
// setTimeout(function(){ });
// client_3.disconnect(); });
// setTimeout(function(){
// done();
// }, 1000);
// }, 1000);
// }
// client_1.on('say', listener);
// client_3 = io.connect(socketURL, io_options);
// });


// it('I can get my connection details', function(done){ it('I can get my connection details', function(done){
// makeSocketRequest(client_1, "detailsView", {}, function(response){ client_1.detailsView(function(response){
// response.should.be.an.instanceOf(Object); response.should.be.an.instanceOf(Object);
// response.status.should.equal("OK") response.status.should.equal("OK")
// response.details.connectedAt.should.be.within(0, new Date().getTime()) response.details.connectedAt.should.be.within(0, new Date().getTime())
// response.details.room.should.equal("defaultRoom") response.details.room.should.equal("defaultRoom")
// done() done()
// }); });
// }); });


// it('Clients can talk to each other', function(done){ it('Clients can talk to each other', function(done){
// var listener = function(response){ client_1.events.say = function(response){
// client_1.removeListener('say', listener); delete client_1.events.say;
// response.should.be.an.instanceOf(Object); response.should.be.an.instanceOf(Object);
// response.context.should.equal('user'); response.context.should.equal('user');
// response.message.should.equal('hello from client 2'); response.message.message.should.equal('hello from client 2');
// done(); done();
// } }
// client_1.on('say', listener); client_2.say({message: "hello from client 2"});
// client_2.emit("say", {message: "hello from client 2"}); });
// });


// it('can run actions with errors', function(done){ it('can run actions with errors', function(done){
// makeSocketRequest(client_1, "action", {action: "cacheTest"}, function(response){ client_1.action('cacheTest', function(response){
// response.should.be.an.instanceOf(Object); response.should.be.an.instanceOf(Object);
// response.error.should.equal("Error: key is a required parameter for this action"); response.error.should.equal("Error: key is a required parameter for this action");
// done(); done();
// }); });
// }); });


// it('can run actions', function(done){ it('can run actions properly', function(done){
// makeSocketRequest(client_1, "action", {action: "cacheTest", key: "test key", value: "test value"}, function(response){ client_1.action("cacheTest", {key: "test key", value: "test value"}, function(response){
// response.should.be.an.instanceOf(Object); response.should.be.an.instanceOf(Object);
// should.not.exist(response.error); should.not.exist(response.error);
// done(); done();
// }); });
// }); });


// it('will limit how many simultanious connections I can have', function(done){ it('will limit how many simultanious connections I can have', function(done){
// this.timeout(5000) this.timeout(5000);
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 500}});
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 600}});
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 700}});
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 800}});
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 900}});
// client_1.emit('action', {action: 'sleepTest', params: {sleepDuration: 1000}});

// var responses = []
// var checkResponses = function(data){
// responses.push(data);
// if(responses.length == 6){
// for(var i in responses){
// var response = responses[i];
// if(i == 0){
// response.error.should.eql("you have too many pending requests");
// }else{
// should.not.exist(response.error)
// }
// }


// client_1.removeListener('response', checkResponses); var responses = [];
// done(); client_1.action('sleepTest', {sleepDuration: 500}, function(response){ responses.push(response); })
// } client_1.action('sleepTest', {sleepDuration: 600}, function(response){ responses.push(response); })
// } client_1.action('sleepTest', {sleepDuration: 700}, function(response){ responses.push(response); })
client_1.action('sleepTest', {sleepDuration: 800}, function(response){ responses.push(response); })
client_1.action('sleepTest', {sleepDuration: 900}, function(response){ responses.push(response); })
client_1.action('sleepTest', {sleepDuration: 1000}, function(response){ responses.push(response); })


// client_1.on('response', checkResponses); setTimeout(function(){
// }); responses.length.should.equal(6);
for(var i in responses){
var response = responses[i];
if(i == 0){
response.error.should.eql("you have too many pending requests");
}else{
should.not.exist(response.error)
}
}
done();
}, 2000);

});


// it('can change rooms and get room details', function(done){ // it('can change rooms and get room details', function(done){
// client_1.emit("roomChange", {room: "otherRoom"}); // client_1.emit("roomChange", {room: "otherRoom"});
Expand Down

0 comments on commit efb26e7

Please sign in to comment.