du9826 2014-06-19 15:45
浏览 67

在PHP中使用Reactphp作为套接字,端口停止侦听

I'm developing an iOS app that has a chat system included (using these instructions). The api runs on a LAMP server, so I used ReactPhp instead of Twisted for python as socket handler to communicate between client/server and server/client.

This is the code I'm using for the file socket.php

<?
require 'vendor/autoload.php';

$port = 1337;
$host = '127.0.0.1';

$app = function ($request, $response) {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end('Hello '.time()."
");
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo 'Server running at http://'.$host.':'.$port."
";

$socket->listen($port, $host);
$loop->run();
?>

Then I just run this on the terminal:

php socket.php

The issues:

  1. It works fine, but... if I close the terminal (or stop the process with ctrl+Z), the port stops listening. How do I get the socket to listen all the time from the moment Apache starts?

  2. After closing the terminal (case 1), if I try running php socket.php again, I get this message: Could not bind to tcp://127.0.0.1:1337: Address already in use - If the port is already in use, then why am I unable to access it after closing the terminal?

  • 写回答

2条回答 默认 最新

  • dongre6270 2016-11-17 09:15
    关注

    It seems that php thread still running. From terminal:

    ps -ef | grep php
    

    Output:

    501  7286  3848   0 10:09AM ttys002    0:00.08 php socket.php
    

    Kill thread:

    kill 7286
    

    Update for Nino: The second issue can happend using nohup command too.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line