I have done Laravel/PHP web server and run it on http://120.77.44.4/ Now I need to calculate Network Speed in JavaScript. To implement this, I tried to send a HTTP request to the server and calculate the response data size and time. When I divide the response data size by time, I can get the download speed of the client. I tried it myself, but I can not get as I need.
This is the JavaScript function in php code.
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://120.77.44.4/RequestSpeed", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
alert('OK, 200');
} else if (xmlhttp.status == 400) {
alert('There was an error 400');
} else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.send();
This is the code in server.
public function RequestSpeed() {
file_get_contents('./test.txt', true);
return response();
}
The server was done by using Laravel/MySql.