【问题标题】:Starting Pure PHP socket in Backend/Asynchronously在后端/异步中启动纯 PHP 套接字
【发布时间】:2015-06-01 03:31:54
【问题描述】:

我为我的 ios 应用程序创建了一个 php 套接字。当我在完成所有工作之前使用终端启动它时,它运行良好..

问题出在第一个 socket_write。

写之前先检查一下服务器的socket是否在运行..$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 启动套接字时,它不会断开连接。

提前谢谢你...

【问题讨论】:

    标签: php sockets asynchronous


    【解决方案1】:

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

    这是我现在的样子:

    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 &");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      相关资源
      最近更新 更多