Last active
March 12, 2021 20:49
-
-
Save CViniciusSDias/1eaf3ef1155a4168436da4d43d2e701f to your computer and use it in GitHub Desktop.
Estudo sobre stream_select
This file contains 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
<?php | |
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr); | |
if (!$socket) { | |
var_dump($errno, $errstr); | |
exit(1); | |
} | |
while ($con = stream_socket_accept($socket)) { | |
$timeout = rand(1, 5); | |
sleep($timeout); | |
fwrite($con, "Socket com espera de $timeout segundos"); | |
fclose($con); | |
} | |
fclose($socket); |
This file contains 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
<?php | |
$streamList = [ | |
// Socket that takes rand(1, 5) seconds to respond | |
stream_socket_client("tcp://0.0.0.0:8000", $errno, $errstr), | |
fopen('teste.txt', 'r'), | |
]; | |
foreach ($streamList as $stream) { | |
stream_set_blocking($stream, false); | |
} | |
do { | |
$streamCopy = $streamList; | |
$select = stream_select($streamCopy, $write, $except, 0, 200000); | |
if ($select === 0) continue; | |
foreach ($streamCopy as $readStream) { | |
unset($streamList[array_search($readStream, $streamList)]); | |
echo stream_get_contents($readStream) . PHP_EOL; | |
} | |
} while(!empty($streamList)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment