每隔60秒,我就想执行一些其他例程。在这次活动中,我也希望有机会重新建立连接,如果它失去了任何原因。如何做到这一点呢?
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, "123.321.123.321" , 20);
socket_listen ($sock , 10);
while (true)
{
//Do some other routine ever 60 seconds
$client = socket_accept($sock); //Blocking function!
$input = socket_read($client, 1024000);
$response=processInput($input);
socket_write($client, $response);
}
使用stream_select()函数
例如:$socket = stream_socket_server("tcp://0.0.0.0:8160", $errno,$errorMessage);
$master[] = $socket;
$read = $master;
$write_something = false;
$mod_fd = stream_select($read, $_w, $_e, $write_something ? 0 : 1, 5000);