理解 PHP socket 系列函数 2

实现多个用户连接

 $socket) {
        if ($socket == $master) { // 新的客户端连接(它等于 socket_create 创建的资源)
            echo "accept \n";
            array_push($sockets,socket_accept($socket));
        } else { // 已连接的客户端
            echo "read \n";
            if (($buf = socket_read($socket, 2048, $readType)) != '') {
                if (!isset($handShake[$index])) {
                    echo $buf;
                    echo "\n";
                    doHandShake($socket, $buf);
                    $handShake[$index] = true;
                } else {
                    echo decode($buf);
                    $response = 'This is response';
                    $response = encode($response);
                    socket_write($socket, $response, strlen($response));
                    echo "\n";
                }
            }
        }
    }
    echo "end while\n";
}

发表评论