在Backend/Asynchronous中启动Pure PHP套接字


Starting Pure PHP socket in Backend/Asynchronously

我已经为我的ios应用程序创建了一个php套接字。当我在做所有工作之前使用终端启动它时,它工作得很好。。

问题是第一个套接字写入。

在写入之前,请检查服务器的套接字是否正在运行。。$result = socket_connect($socket, $address, $service_port);

如果false,我需要重定向到页面http://localhost/api3/server.php以启动套接字

public function __socket_write($text)
{
    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) 
    {
        // if failed to connect means the server is currently down/not running
        $this->__socket_start("http://localhost/api3/server.php");
    }
    // codes here not called api got an time out error as well, must have been    redirected completely?
}
public function __socket_start($url, $status_code = 303)
{
   header('Location: '.$url, true, $status_code);
}

服务器已成功启动,但看起来我需要异步调用__socket_start()才能执行$this->__socket_start()以下的代码,但我不知道如何执行,过了一段时间,服务器的套接字再次关闭。。。

当我使用端子php server.php启动插座时,它不会断开。

提前谢谢。。。

解决了这个问题,它不是我真正想做的,但它现在可以正常工作,就像我从一开始就打算做的那样。。

以下是我现在的样子:

public function __socket_write($text)
{
    $service_port = xxxxxx;
    $address = MyAddress;
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) 
    {
        $this->__socket_start();
    } 
    @socket_listen($socket);
    ...
    socket_close($socket);
}
public function __socket_start()
{
    $cmd = "php server.php";
    shell_exec($cmd." > /dev/null 2>/dev/null &");
}