I've been having a hard time writing websockets in PHP, so I decided to try to download a datastream. It works, I can get content in different times, parse it, and use it, but the entire response is saved into memory...
Is there a way to reset the request's response every time in onreadystatechange
function? (where that comment is)
Removing the comment, I get this error:
test.html:20 Uncaught TypeError: Cannot assign to read only property 'responseText' of object '#'
A working code for streaming:
class HTTPStream {
constructor(url, callback, error) {
this.request = new XMLHttpRequest();
var previous_text = '';
if (typeof error === "function")
this.request.onerror = error;
this.request.onreadystatechange = () => {
if (this.request.readyState > 2) {
var new_response = this.request.responseText.substring(previous_text.length);
if(new_response != "") {
var result = JSON.parse(new_response);
callback(result);
}
previous_text = this.request.responseText;
//this.request.responseText = "";
}
};
this.request.open("GET", url, true);
this.request.send();
}
cancel() {
this.request.abort();
}
}
new HTTPStream('test.php', (m) => console.log(m));
EDIT:
By suggestions, I tried doing this, but unfortunately you can set it to writable only once, and that is it. I have lots of outputs, not just once.