想用php搞个server,存储所有连接的客户端的socket,并通过它们和客户端交互。当客户端
第一次连接时,将它们存在数组中。之后要实现客户端一旦发送数据,服务器就要接收并反应。
do{
//新的客户端上线
if ($client_socket = socket_accept($sock)) {
if ($identifyMsg = $this->read($client_socket)) {
$this->judgeAndDo($client_socket,$identifyMsg);
}
}
//读取已存在的客户端发来的信息
foreach ($this->classes_list as $item) {
//遍历教师端
echo "Search the teacher!";
if($identifyMsg = $this->read($item->teaSock)){
$this->judgeAndDo(null,$identifyMsg);
}
//遍历学生端
foreach ($item->absStuArray as $subItem) {
echo "Search the teacher!";
if ($identifyMsg = $this->read($subItem->stuSock)) {
$this->judgeAndDo(null,$identifyMsg);
}
}
}
}while (true);
在读取已存在的客户端发来的信息时,似乎一直阻塞在socket-read()中(提前在类中封装成了read()方法),有没有办法在没有数据可读的情况下直接跳过?