Reputation: 23500
Following this information:
I was hoping to achieve the following:
By doing:
$.ajax({
type: "POST",
url: "post.php",
async: true,
cache: false,
data: { data : 'ping 127.0.0.1' },
success: function(msg) {
console.log(msg);
}
});
With PHP looking like:
//header("Connection: close");
//header("Content-Type: application/octet-stream");
socket_write($socket, 'ping 127.0.0.1', strlen('ping 127.0.0.1'));
ignore_user_abort();
//ob_implicit_flush(true);
//ob_end_flush();
while ($out = socket_read($socket, 8192)) {
echo $out;
ob_flush();
}
//ob_start();
And Python simply doing:
sock.recv(8192)
for i in range(0, 4):
sock.send(bytes(json.dumps('Ping successfull..'), 'UTF-8'))
sleep(0.5)
I thought I'd be able to buffer out the data in "real-time" instead of ending up with the data bunched up like this:
"Ping successfull..""Ping successfull..""Ping successfull..""Ping successfull.."
Where did i go wrong in my thinking? This should be quite straight forward? I've done it so many times but yet this time I can't figure out what i'm doing wrong :/
I've also verified that it is indeed the AJAX/PHP bridge not working, PHP recieves the data as it arrives, verified with strftime("%H:%M:%S")
and i get the expected difference in between each recieves.
Upvotes: 1
Views: 1566
Reputation: 163
AJAX uses HTTP, so the callback for success/failure is only called when the request completes and there are no options for getting the data while it's being received. You should check out web sockets, which will allow you to handle streaming data from the server.
Edit: You could also look at multipart/x-mixed-replace
and EventSource
Upvotes: 1