有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);
}
答案 0 :(得分:0)
使用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);