dongxia9519 2017-03-07 09:50
浏览 56

如何使用php创建异步套接字服务器?

I'm trying to create a Asynchronous socket server to handle socket client, send message to client when it's needs. I use reactphp library to implement it but I can't do correct, my server still is blocked. I'm new in this library, please help.

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
    $conn->pipe($conn);
});
echo "Socket server listening on port 4000.
";
echo "You can connect to it by running: telnet localhost 4000
";
$socket->listen(4200);
$loop->run();


// code bottom doesn't run because of blocking socket
  • 写回答

1条回答

  • dsf12313 2017-03-12 12:17
    关注

    $loop->run(); runs the event loop and will never return unless you stop the loop. The loop is your scheduler and invokes your event handlers in case events occur.

    If you want to react to events, you have to register event listeners before you run the loop.

    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件