websocket opened whilst logging in

This commit is contained in:
hydrabolt
2015-08-15 23:14:40 +01:00
parent 9ce332563b
commit 4b5e229df9

View File

@@ -138,6 +138,8 @@ exports.Client.prototype.login = function( email, password ) {
var time = Date.now(); var time = Date.now();
self.connectWebsocket();
Internal.XHR.login( email, password, function( err, token ) { Internal.XHR.login( email, password, function( err, token ) {
if ( err ) { if ( err ) {
@@ -148,8 +150,8 @@ exports.Client.prototype.login = function( email, password ) {
} else { } else {
self.token = token; self.token = token;
self.loggedIn = true; self.loggedIn = true;
self.connectWebsocket();
console.log("Took "+ (Date.now() - time) +" ms to login!"); console.log("Took "+ (Date.now() - time) +" ms to login!");
self.websocket.sendData();
} }
} ); } );
@@ -173,6 +175,8 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
var time = Date.now(); var time = Date.now();
var sentInitData = false;
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB ); this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
this.websocket.onclose = function( e ) { this.websocket.onclose = function( e ) {
self.triggerEvent( "disconnected", [ { self.triggerEvent( "disconnected", [ {
@@ -347,20 +351,26 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
} }
this.websocket.onopen = function() { this.websocket.onopen = function() {
var connDat = { this.sendData();
op: 2,
d: {
token: self.token,
v: 2
}
};
connDat.d.properties = Internal.WebSocket.properties;
this.sendPacket( connDat );
console.log("Took "+ (Date.now() - time) +" ms to open WS connection!"); console.log("Took "+ (Date.now() - time) +" ms to open WS connection!");
time = Date.now(); time = Date.now();
} }
this.websocket.sendData = function(){
if(this.readyState == 1 && !sentInitData && self.token){
sentInitData = true;
var connDat = {
op: 2,
d: {
token: self.token,
v: 2
}
};
connDat.d.properties = Internal.WebSocket.properties;
this.sendPacket( connDat );
}
}
} }
exports.Client.prototype.logout = function( callback ) { exports.Client.prototype.logout = function( callback ) {