Block page loading while WebSocket is not connected

This commit is contained in:
Pierre HUBERT 2020-04-01 10:23:45 +02:00
parent 5eee376136
commit 321a91910f
3 changed files with 30 additions and 8 deletions

View File

@ -11,7 +11,7 @@ ComunicWeb.common.system = {
* @param {String} openPage Specify a page to open * @param {String} openPage Specify a page to open
* @return {Boolean} True for a success * @return {Boolean} True for a success
*/ */
init: function(openPage){ init: async function(openPage){
//Display Comunic logo //Display Comunic logo
ComunicWeb.debug.displayComunicLogo(); ComunicWeb.debug.displayComunicLogo();
@ -66,7 +66,13 @@ ComunicWeb.common.system = {
/** /**
* What to do after login refresh * What to do after login refresh
*/ */
var afterLoginRefresh = function(){ var afterLoginRefresh = async function(){
// Initialize Websocket if user is connected
if(signed_in()) {
await UserWebSocket.Connect();
await UserWebSocket.WaitForConnected();
}
/** /**
* Open a page * Open a page
@ -79,10 +85,6 @@ ComunicWeb.common.system = {
//Open specified page //Open specified page
ComunicWeb.common.page.openPage(openPage); ComunicWeb.common.page.openPage(openPage);
// Initialize Websocket if user is connect
if(signed_in())
UserWebSocket.Connect();
//End of init //End of init
ComunicWeb.debug.logMessage("Application is ready !"); ComunicWeb.debug.logMessage("Application is ready !");
} }

View File

@ -56,6 +56,22 @@ class UserWebSocket {
} }
} }
/**
* Wait for the socket to be connected (if not already)
*/
static WaitForConnected() {
return new Promise((res, err) => {
// Check if we are already connected
if(this.ws.readyState == WebSocket.OPEN) {
res();
return;
}
this.ws.addEventListener("open", () => res());
});
}
/** /**
* Handles websocket errors * Handles websocket errors
*/ */
@ -70,6 +86,9 @@ class UserWebSocket {
static async Closed(e) { static async Closed(e) {
console.error("WS closed", e) console.error("WS closed", e)
// Reset requests queue
requests = {};
// Check if the server was gracefully stopped // Check if the server was gracefully stopped
if(!this.hasOwnProperty("ws")) if(!this.hasOwnProperty("ws"))
return; return;

View File

@ -151,7 +151,7 @@ ComunicWeb.user.userLogin = {
}; };
//What to do after the request is completed //What to do after the request is completed
var afterAPIrequest = function(result){ const afterAPIrequest = async function(result){
//Prepare data return //Prepare data return
var loginstate = false; var loginstate = false;
@ -178,7 +178,8 @@ ComunicWeb.user.userLogin = {
ComunicWeb.components.mailCaching.set(usermail); ComunicWeb.components.mailCaching.set(usermail);
// Initialize websocket // Initialize websocket
UserWebSocket.Connect(); await UserWebSocket.Connect();
await UserWebSocket.WaitForConnected();
} }
//Perform next action if login failed //Perform next action if login failed