-
-
Save 6ewis/4584f5456660d84a688e72b2cec32cbf to your computer and use it in GitHub Desktop.
React native check internet speed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const fetchParallel = (link,header, callback) =>{ | |
const POOR = 150; | |
const MODERATE = 550; | |
const GOOD = 2000; | |
const _start = new Date().getTime(); | |
const speedTester = RNFetchBlob | |
.config({ | |
fileCache : true | |
}) | |
.fetch('GET', SPEED_TESTER_FILE, { }); // SPEED_TESTER_FILE some file address. try to download a file size of > 700kb | |
const execution = Promise.all([ | |
fetch(link,header), | |
speedTester | |
]); | |
execution.then(([req, speed]) => { | |
return Promise.all([req.json(), speed.path()]) | |
}) | |
.then(([req, res]) => { | |
callback(req); | |
const _end = new Date().getTime(); | |
const kbPerSecond = Math.floor(1024/((_end-_start)/1000)); | |
var slow = false; | |
var good = false; | |
if(kbPerSecond <= POOR){ | |
// very bad connection | |
Warning('very bad connection'); | |
slow = true; | |
}else if(kbPerSecond >= POOR && kbPerSecond <= MODERATE){ | |
// avg speed | |
Warning('avg speed'); | |
slow = true; | |
}else if(kbPerSecond >= MODERATE && kbPerSecond <= GOOD){ | |
// good connection | |
Warning('good connection'); | |
good = true; | |
}else if(kbPerSecond > GOOD){ | |
// excellent connection | |
Warning('excellent connection'); | |
good = true; | |
}else{ | |
// unknown type | |
Warning('unknown type'); | |
slow = true; | |
} | |
RNFetchBlob.fs.unlink(res); | |
if(slow){ | |
} | |
if(good){ | |
} | |
}).catch(error=>{ | |
// alert error | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment