我在服务器端运行这个循环:
while (true) {
$changed = $this->sockets;
socket_select($changed, $write = NULL, $except = NULL, NULL);
foreach($changed AS $socket) {
if($socket == $this->master) {
$client = socket_accept($this->master);
if($client < 0) {
echo "socket_accept failed";
continue;
} else {
$this->connect($client);
}
} else {
$bytes = @socket_recv($socket, $buffer, 2048, 0);
if($bytes == 0) {
$this->close($socket);
} else {
$user = $this->get_user_by_socket($socket);
if(!$user->handshake) {
$this->do_handshake($user, $buffer);
} else {
$this->process($user, $buffer);
}
}
}
}
}基本上它会在执行任何可以发送给所有客户端或者单个客户端的操作之前,等待套接字内的任何内容进行更改。我想要做的是能够在一段时间不活动之后将数据推送给客户......像游戏中的倒数计时器一样说。因此,如果用户没有任何操作,5秒后自动为该用户发送操作。我会怎么做呢?我尝试过使用存储在其中的time(),然后检查数学运算的last_update,但是,只有在插槽发生更改时才会运行我在while(true)循环内执行的任何操作,该更改来自用户结束...
我想我只是超级输了。 :)
谢谢!