Ping server after setting app offline/online. Also include timeout (#1739)
* Ping server after setting app offline/online. Also include timeout * Add missing semicolon
This commit is contained in:
parent
1cddb45905
commit
c63ca6a7e0
1 changed files with 9 additions and 1 deletions
|
|
@ -1,7 +1,11 @@
|
|||
import {NetInfo} from 'react-native';
|
||||
|
||||
import RNFetchBlob from 'react-native-fetch-blob';
|
||||
|
||||
import {Client4} from 'mattermost-redux/client';
|
||||
|
||||
const PING_TIMEOUT = 10000;
|
||||
|
||||
export async function checkConnection(isConnected) {
|
||||
if (Client4.getBaseRoute() === '/api/v4') {
|
||||
// If we don't have a server yet, return the default implementation
|
||||
|
|
@ -12,7 +16,7 @@ export async function checkConnection(isConnected) {
|
|||
const server = `${Client4.getBaseRoute()}/system/ping?time=${Date.now()}`;
|
||||
|
||||
try {
|
||||
await fetch(server, {method: 'get'});
|
||||
await RNFetchBlob.config({timeout: PING_TIMEOUT}).fetch('GET', server);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
|
|
@ -21,6 +25,10 @@ export async function checkConnection(isConnected) {
|
|||
|
||||
function handleConnectionChange(onChange) {
|
||||
return async (isConnected) => {
|
||||
// Set device internet connectivity immediately
|
||||
onChange(isConnected);
|
||||
|
||||
// Check if connected to server
|
||||
const result = await checkConnection(isConnected);
|
||||
onChange(result);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue