Browse files

connection process sorted out

  • Loading branch information...
1 parent 4715e07 commit a9faaf887bb62e0b3447319989801e162307590c @evantahler committed Mar 7, 2013
Showing with 49 additions and 17 deletions.
  1. +25 −6 examples/clients/web/actionHeroWebSocket.js
  2. +2 −1 examples/clients/web/webSockets.html
  3. +22 −10 test/client_webSockets.js
View
31 examples/clients/web/actionHeroWebSocket.js
@@ -15,14 +15,25 @@
for(var i in options){
self.options[i] = options[i];
}
+
+ if(options && options.faye != null){
+ self.faye = options.faye;
+ }else if(window.Faye != null){
+ self.faye = window.Faye;
+ }else{
+ self.faye = Faye;
+ }
}
actionHeroWebSocket.prototype.defaults = function(){
+ var host;
+ if(typeof window != 'undefined'){ host = window.location.origin; }
return {
- host: window.location.origin,
+ host: host,
path: "/faye",
setupChannel: "/_welcome",
- channelPrefix: "/client/websocket/connection/"
+ channelPrefix: "/client/websocket/connection/",
+ startupDelay: 500
}
}
@@ -41,17 +52,23 @@
actionHeroWebSocket.prototype.connect = function(callback){
var self = this;
- self.client = new Faye.Client(self.options.host + self.options.path);
+ self.client = new self.faye.Client(self.options.host + self.options.path);
var initialMessage = self.client.publish(self.options.setupChannel, 'hello');
initialMessage.callback(function() {
- self.id = self.client.getClientId()
+ self.id = self.client.getClientId();
self.channel = self.options.channelPrefix + self.id;
self.subscription = self.client.subscribe(self.channel, function(message) {
self.handleMessage(message);
});
- callback(null, self);
+
+ setTimeout(function(){
+ self.detailsView(function(details){
+ callback(null, details);
+ });
+ }, self.options.startupDelay);
+
});
initialMessage.errback(function(error) {
@@ -134,4 +151,6 @@
this.send({event: 'silenceRoom', room: room}, callback);
}
-})(typeof exports === 'undefined'? this['mymodule']={}: exports);
+ exports.actionHeroWebSocket = actionHeroWebSocket;
+
+})(typeof exports === 'undefined' ? window : exports);
View
3 examples/clients/web/webSockets.html
@@ -16,11 +16,12 @@
}
}
- A.connect(function(err){
+ A.connect(function(err, details){
if(err != null){
A.log(err);
}else{
A.log("CONNECTED");
+ A.log(details);
}
});
View
32 test/client_webSockets.js
@@ -4,9 +4,10 @@ describe('Client: Web Sockets', function(){
var should = require("should");
var socketURL = "http://localhost:9000";
var faye = require("faye");
- var client_1 = new faye.Client(socketURL);
- var client_2 = new faye.Client(socketURL);
- var client_3 = new faye.Client(socketURL);
+ var actionHeroWebSocket = require(process.cwd() + "/public/javascript/actionHeroWebSocket.js").actionHeroWebSocket;
+ var client_1 = new actionHeroWebSocket({host: socketURL, faye: faye});
+ var client_2 = 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){
@@ -30,20 +31,31 @@ describe('Client: Web Sockets', function(){
before(function(done){
specHelper.prepare(0, function(api){
api.redis.client.flushdb(function(){
- apiObj = specHelper.cleanAPIObject(api);
+ apiObj = api;
done();
});
})
});
+ it('faye should work in general', function(done){
+ var client = new faye.Client(socketURL + "/faye");
+ client.subscribe('/test', function(message){
+ message.message.should.equal('hello');
+ done();
+ });
+
+ setTimeout(function(){
+ apiObj.faye.client.publish("/test", {message: 'hello'});
+ }, 1000);
+ });
+
it('socket client connections should work: client 1', function(done){
- client_1.on('welcome', function(data){
+ client_1.connect(function(err, data){
data.should.be.an.instanceOf(Object);
- data.context.should.equal("api");
- data.room.should.equal("defaultRoom");
- setTimeout(function(){
- done();
- }, 1000);
+ data.context.should.equal("response");
+ data.details.room.should.equal("defaultRoom");
+ data.details.totalActions.should.equal(0);
+ done();
});
});

0 comments on commit a9faaf8

Please sign in to comment.