让PHP服务器脚本持久监听套接字是否阻塞?

时间:2014-02-24 18:32:44

标签: javascript php ajax sockets asynchronous

我正在编写一个分为两部分的项目。

到目前为止,我有一个前端View.php(HTML5,CSS3,JQuery),这将查询server.php

服务器PHP打开一个到服务器的TCP套接字并监听并可以通过写入套接字来发出命令。

正常程序现在就像这样

View.php -> Calls using rest API to server.php
Server.php -> Connects to TCP -> Reads from TCP -> Json_encodes & print -> close TCP socket connection.

我想要实现的是曾经启动的脚本Server.php。它会不断地监听服务器,直到它获得关机命令。我想保持fsocket连接打开。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

答案是使用非阻塞编程。在PHP中,我们具有非阻塞I / O的特定功能。对于套接字,您应该在套接字资源上使用socket_set_nonblock函数。

$port = 8081;
$address = '127.0.0.1';

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    exit();
}
if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    exit();
}
if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
socket_set_nonblock($sock);
echo "listening for new connection".PHP_EOL;
$conneted_clients = [];

do {
   $clientsock = socket_accept($sock);
   if($clientsock !== false){

        socket_set_nonblock($clientsock);
        $conneted_clients[] = $clientsock;
        socket_getpeername($clientsock,$address);
        echo "New Connection from: ".$address.PHP_EOL;

        $msg = PHP_EOL."Welcome to the PHP Test Server. " . PHP_EOL.
        "To quit, type 'quit'. To shut down the server type 'shutdown'." . PHP_EOL;

        socket_write($clientsock, $msg, strlen($msg));
    }
    $status = check_clients($conneted_clients);

    if(!$status) break;
    usleep(500000);
} while (true);

function check_clients($clients)
{
    foreach($clients as $key => $con)
    {
        if(get_resource_type($con) !== "Socket")
        {   
            socket_getpeername($clientsock,$address);
            echo $address." has diconnected.".PHP_EOL;
            unset($clients[$key]);
            continue;
        }
        if (false === $buff = socket_read($con, 2048)) {
            continue;
        }

        $buff = trim($buff);

        if ($buff == 'quit') {
            socket_close($con);
            unset($clients[$key]);
            continue;
        }
        if (trim($buff) == 'shutdown') {
            socket_close($con);
            echo "shutdown initiated".PHP_EOL;
            return FALSE;
        }
        if($buff != false || $buff != null)
        {
            $talkback = "PHP: You said '$buff'.".PHP_EOL;
            socket_write($con, $talkback, strlen($talkback));
            echo "$buff".PHP_EOL;
        }
    }
    return TRUE;
}
echo "Closing Server";
socket_close($sock);