Question

I have some code like the following:

$socket = stream_socket_client('tcp://my.domain.com:2082', $errno, $errstr, 1, STREAM_CLIENT_ASYNC_CONNECT);
fwrite($socket, "\xfe\x01");
$data = fread($socket, 1024);

This code is executed when a HTML web page loads, the line where fread is used takes just over a second as can be seen:

1374008598.18 : Read begin
1374008599.75 : Read end

This incurs a great increase in page loading time, is there any way to execute the fread() command asynchronously. Considering that PHP is a server side language I'm not sure that it would be possible in this case, if not is there another solution to prevent this large increase in loading time as a result of fread()

Was it helpful?

Solution

The only way to improve this, is to return the html early and then use javascript to perform an AJAX request for another page which does the fread().. You could display a progress bar until the operation has finished.

Btw, for sockets there is socket_read()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top