Skip to content

Commit

Permalink
#36 improving sockets setup in auth cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jan 13, 2013
1 parent c0c9181 commit 7d5c134
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,15 @@ var crudr, socket;
secret : false
},
init : function(){

var self = this;
this.token = false;

this.promise = new Promise();
this.promise.add(function(){
socket = io.connect('{{host}}');
});
//this.promise.add( this.sockets );

// this does nothing??
this.status("initialized");
/*
// main sockets switch
socket.on('connect', function(){
SOCKETS = true;
});

socket.on('disconnect', function(){
SOCKETS = false;
});
*/
},
connect: function( options, callback){
var self = this;
Expand Down Expand Up @@ -137,16 +125,25 @@ var crudr, socket;
case "connected":
this.state.socket = true;
break;
case "disconnected":
this.state.socket = false;
break;
};

// log the event...
var log = this.options.log;
if( log ) this.log(flag);

// check if the right flags are in place
if( this.state.auth && this.state.deps && !this.state.socket ) {
// loa fthe callback
this.promise.resolve();
// maybe this needs to move on socket connection...
this.status("connected");
if( this.state.auth && this.state.deps ) {
// load the sockets
this.sockets();
}

if( this.state.socket ) {
// load the callback
this.promise.resolve();
}
}

}
Expand Down Expand Up @@ -232,6 +229,29 @@ var crudr, socket;

};

CRUDr.prototype.sockets = function(){
var self = this;

// initiate handshake
socket = io.connect('{{host}}');

// main sockets switch
socket.on('connect', function(){

socket.emit('token', self.token);
socket.on('ready', function () {
self.status("connected");
});
});

socket.on('disconnect', function(){

self.status("disconnected");

});

};

// Based on the Paul Irish's log(): http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
CRUDr.prototype.log = function(){
this.history = this.history || []; // store logs to an array for reference
Expand Down

0 comments on commit 7d5c134

Please sign in to comment.