|
|
@ -45,27 +45,103 @@ export class ApiService {
|
|
|
|
protected db: DatabaseService,
|
|
|
|
protected db: DatabaseService,
|
|
|
|
protected connection: ConnectionService,
|
|
|
|
protected connection: ConnectionService,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
connection.monitor().subscribe(isConnected => {
|
|
|
|
this.startOfflineObserver();
|
|
|
|
if ( !isConnected ) {
|
|
|
|
}
|
|
|
|
this.makeOffline();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
protected stopOfflineObserverClosure?: any;
|
|
|
|
this.checkOnline().then(isOnline => {
|
|
|
|
|
|
|
|
if ( isOnline ) {
|
|
|
|
public stopOfflineObserver() {
|
|
|
|
this.makeOnline();
|
|
|
|
if ( this.stopOfflineObserverClosure ) {
|
|
|
|
} else {
|
|
|
|
this.stopOfflineObserverClosure();
|
|
|
|
this.makeOffline();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.checkOnline().then(isConnected => {
|
|
|
|
protected startOfflineObserver() {
|
|
|
|
if ( !isConnected ) {
|
|
|
|
const passiveCheckTime = 5;
|
|
|
|
this.makeOffline();
|
|
|
|
const checkTimes = [5, 5, 10, 10, 15, 15, 20, 20, 30, 30, 30, 60, 60, 500];
|
|
|
|
|
|
|
|
let currentCheckTimeIndex = 0;
|
|
|
|
|
|
|
|
let hasNetConnection = true;
|
|
|
|
|
|
|
|
let hasServerConnection = true;
|
|
|
|
|
|
|
|
let online = true;
|
|
|
|
|
|
|
|
let passiveCheckInterval;
|
|
|
|
|
|
|
|
let activeCheckInterval;
|
|
|
|
|
|
|
|
let stopped = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.stopOfflineObserverClosure = () => {
|
|
|
|
|
|
|
|
stopped = true;
|
|
|
|
|
|
|
|
clearInterval(passiveCheckInterval);
|
|
|
|
|
|
|
|
clearInterval(activeCheckInterval);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkServerConnection = async () => {
|
|
|
|
|
|
|
|
if ( !hasNetConnection ) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.checkOnline();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const startPassiveCheck = () => {
|
|
|
|
|
|
|
|
passiveCheckInterval = setInterval(async () => {
|
|
|
|
|
|
|
|
if ( hasNetConnection ) {
|
|
|
|
|
|
|
|
const server = await checkServerConnection();
|
|
|
|
|
|
|
|
if ( server !== hasServerConnection ) {
|
|
|
|
|
|
|
|
hasServerConnection = server;
|
|
|
|
|
|
|
|
await handleNetConnectionEvent();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, passiveCheckTime * 1000);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const doActiveCheck = async () => {
|
|
|
|
|
|
|
|
if ( activeCheckInterval ) {
|
|
|
|
|
|
|
|
clearInterval(activeCheckInterval);
|
|
|
|
|
|
|
|
if ( currentCheckTimeIndex < (checkTimes.length - 1) ) {
|
|
|
|
|
|
|
|
currentCheckTimeIndex += 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( hasNetConnection ) {
|
|
|
|
|
|
|
|
const server = await checkServerConnection();
|
|
|
|
|
|
|
|
if ( server !== hasServerConnection ) {
|
|
|
|
|
|
|
|
hasServerConnection = server;
|
|
|
|
|
|
|
|
await handleNetConnectionEvent();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.makeOnline();
|
|
|
|
if ( activeCheckInterval ) {
|
|
|
|
|
|
|
|
activeCheckInterval = setInterval(doActiveCheck, checkTimes[currentCheckTimeIndex] * 1000);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleNetConnectionEvent = async () => {
|
|
|
|
|
|
|
|
if ( stopped ) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( online && (!hasNetConnection || !hasServerConnection) ) {
|
|
|
|
|
|
|
|
online = false;
|
|
|
|
|
|
|
|
await this.makeOffline();
|
|
|
|
|
|
|
|
clearInterval(passiveCheckInterval);
|
|
|
|
|
|
|
|
activeCheckInterval = setInterval(doActiveCheck, checkTimes[currentCheckTimeIndex] * 1000);
|
|
|
|
|
|
|
|
} else if ( !online && (hasNetConnection && hasServerConnection) ) {
|
|
|
|
|
|
|
|
if ( activeCheckInterval ) {
|
|
|
|
|
|
|
|
clearInterval(activeCheckInterval);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
online = true;
|
|
|
|
|
|
|
|
currentCheckTimeIndex = 0;
|
|
|
|
|
|
|
|
await this.makeOnline();
|
|
|
|
|
|
|
|
startPassiveCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.connection.monitor().subscribe(isConnected => {
|
|
|
|
|
|
|
|
hasNetConnection = isConnected;
|
|
|
|
|
|
|
|
handleNetConnectionEvent();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startPassiveCheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public checkOnline(): Promise<boolean> {
|
|
|
|
public checkOnline(): Promise<boolean> {
|
|
|
|