From 9ce332563b1f2ba9f64be583ac1deb11fbee3937 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 15 Aug 2015 23:03:23 +0100 Subject: [PATCH 1/4] log speeds --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index 58e490a0b..c306d84d9 100644 --- a/index.js +++ b/index.js @@ -136,6 +136,8 @@ exports.Client.prototype.login = function( email, password ) { var self = this; + var time = Date.now(); + Internal.XHR.login( email, password, function( err, token ) { if ( err ) { @@ -147,6 +149,7 @@ exports.Client.prototype.login = function( email, password ) { self.token = token; self.loggedIn = true; self.connectWebsocket(); + console.log("Took "+ (Date.now() - time) +" ms to login!"); } } ); @@ -168,6 +171,8 @@ exports.Client.prototype.connectWebsocket = function( cb ) { var self = this; + var time = Date.now(); + this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB ); this.websocket.onclose = function( e ) { self.triggerEvent( "disconnected", [ { @@ -189,6 +194,9 @@ exports.Client.prototype.connectWebsocket = function( cb ) { var data = dat.d; + console.log("Took "+ (Date.now() - time) +" ms to get READY!"); + time = Date.now(); + setInterval( function() { webself.keepAlive.apply( webself ); }, data.heartbeat_interval ); @@ -213,8 +221,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) { if ( cached >= toCache ) { self.ready = true; self.triggerEvent( "ready" ); + console.log("Took "+ (Date.now() - time) +" ms to prepare!"); } } ); + console.log("-", x, Date.now() - time) } } else if ( dat.t === "MESSAGE_CREATE" ) { @@ -348,6 +358,8 @@ exports.Client.prototype.connectWebsocket = function( cb ) { connDat.d.properties = Internal.WebSocket.properties; this.sendPacket( connDat ); + console.log("Took "+ (Date.now() - time) +" ms to open WS connection!"); + time = Date.now(); } } From 4b5e229df9394531f90a80dfc0e439e14939e270 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 15 Aug 2015 23:14:40 +0100 Subject: [PATCH 2/4] websocket opened whilst logging in --- index.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c306d84d9..ebd794bde 100644 --- a/index.js +++ b/index.js @@ -138,6 +138,8 @@ exports.Client.prototype.login = function( email, password ) { var time = Date.now(); + self.connectWebsocket(); + Internal.XHR.login( email, password, function( err, token ) { if ( err ) { @@ -148,8 +150,8 @@ exports.Client.prototype.login = function( email, password ) { } else { self.token = token; self.loggedIn = true; - self.connectWebsocket(); 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 sentInitData = false; + this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB ); this.websocket.onclose = function( e ) { self.triggerEvent( "disconnected", [ { @@ -347,20 +351,26 @@ exports.Client.prototype.connectWebsocket = function( cb ) { } this.websocket.onopen = function() { - var connDat = { - op: 2, - d: { - token: self.token, - v: 2 - } - }; + this.sendData(); - connDat.d.properties = Internal.WebSocket.properties; - - this.sendPacket( connDat ); console.log("Took "+ (Date.now() - time) +" ms to open WS connection!"); 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 ) { From 277e4929c41e3deab99dc78ceb6cf912e6585410 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 15 Aug 2015 23:22:37 +0100 Subject: [PATCH 3/4] small speed fix --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ebd794bde..639d8a17c 100644 --- a/index.js +++ b/index.js @@ -228,7 +228,6 @@ exports.Client.prototype.connectWebsocket = function( cb ) { console.log("Took "+ (Date.now() - time) +" ms to prepare!"); } } ); - console.log("-", x, Date.now() - time) } } else if ( dat.t === "MESSAGE_CREATE" ) { @@ -369,6 +368,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) { connDat.d.properties = Internal.WebSocket.properties; this.sendPacket( connDat ); + time = Date.now(); } } } From 46e3a58c055ed8302cce52e7c49ce13c5a357261 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 15 Aug 2015 23:37:49 +0100 Subject: [PATCH 4/4] removed annoying time update messages --- index.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 639d8a17c..69e3976c0 100644 --- a/index.js +++ b/index.js @@ -136,8 +136,6 @@ exports.Client.prototype.login = function( email, password ) { var self = this; - var time = Date.now(); - self.connectWebsocket(); Internal.XHR.login( email, password, function( err, token ) { @@ -147,11 +145,11 @@ exports.Client.prototype.login = function( email, password ) { reason: "failed to log in", error: err } ] ); + self.websocket.close(); } else { self.token = token; - self.loggedIn = true; - console.log("Took "+ (Date.now() - time) +" ms to login!"); self.websocket.sendData(); + self.loggedIn = true; } } ); @@ -173,8 +171,6 @@ exports.Client.prototype.connectWebsocket = function( cb ) { var self = this; - var time = Date.now(); - var sentInitData = false; this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB ); @@ -198,9 +194,6 @@ exports.Client.prototype.connectWebsocket = function( cb ) { var data = dat.d; - console.log("Took "+ (Date.now() - time) +" ms to get READY!"); - time = Date.now(); - setInterval( function() { webself.keepAlive.apply( webself ); }, data.heartbeat_interval ); @@ -222,10 +215,9 @@ exports.Client.prototype.connectWebsocket = function( cb ) { self.cacheServer( _server, function( server ) { cached++; - if ( cached >= toCache ) { + if ( cached === toCache ) { self.ready = true; self.triggerEvent( "ready" ); - console.log("Took "+ (Date.now() - time) +" ms to prepare!"); } } ); } @@ -350,12 +342,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) { } this.websocket.onopen = function() { - this.sendData(); + this.sendData("onopen"); - console.log("Took "+ (Date.now() - time) +" ms to open WS connection!"); - time = Date.now(); } - this.websocket.sendData = function(){ + this.websocket.sendData = function(why){ if(this.readyState == 1 && !sentInitData && self.token){ sentInitData = true; var connDat = { @@ -368,7 +358,6 @@ exports.Client.prototype.connectWebsocket = function( cb ) { connDat.d.properties = Internal.WebSocket.properties; this.sendPacket( connDat ); - time = Date.now(); } } }