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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建