Created
March 4, 2020 04:40
-
-
Save stafordtituss/5f651696ef19482b755a7d69437246db to your computer and use it in GitHub Desktop.
HTTP Readable Stream using fetch - CLIENT SIDE (HTML and Vanilla JS) < An example for getting artillery report stream >
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Artillery Report Generation</title> | |
</head> | |
<body> | |
<h1>ARTILLERY REPORT GENERATION</h1> | |
<p>Click the button to run the test and generate the report!!</p> | |
<button>RUN</button> | |
<script> | |
const button = document.querySelector('button'); | |
button.addEventListener('click', function() { | |
fetch('http://localhost:8000', {method: 'post'}) | |
.then(response => { | |
const reader = response.body.getReader(); | |
reader.read().then(function loop({done, value}) { | |
const chunk = String.fromCharCode.apply(null, new Uint8Array(value)); | |
console.log(chunk); | |
document.write(chunk); | |
document.write("<br>"); | |
return reader.read().then(loop); | |
}); | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment