php - 从聊天类内部访问 Ratchet 循环
问题描述
我正在尝试使用 Ratchet 已经制作的事件循环。
$server=IoServer::factory(new HttpServer(new WsServer(new class implements MessageComponentInterface{
public function __construct(){
// get the Ratchet Loop
}
public function onOpen(ConnectionInterface $conn){}
public function onMessage(ConnectionInterface $from, $msg){}
public function onClose(ConnectionInterface $conn){}
public function onError(ConnectionInterface $conn, \Exception $e){}
})),123);
我可以通过调用 $server->loop 来获取它,但是我无法将它传递给类构造函数,因为 $server 在自身初始化期间不可访问,我想知道是否有更好的获取方法?
解决方案
您可以实例化一个循环并将其传递给__construct
$loop = React\EventLoop\Factory::create();
$webSock = new React\Socket\Server('127.0.0.1:8080', $loop);
$server = new IoServer(new HttpServer(new WsServer(new class($loop) implements MessageComponentInterface {
protected $loop;
public function __construct(\React\EventLoop\LoopInterface $loop)
{
$this->loop = $loop;
}
public function onOpen(ConnectionInterface $conn)
{
}
public function onMessage(ConnectionInterface $from, $msg)
{
}
public function onClose(ConnectionInterface $conn)
{
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
}
})), $webSock);
$loop->run();
在这种方法中不能使用IoServer::factory
. 因为您需要将自己的循环传递给$webSock
andIoServer
推荐阅读
- php - PHP Wordpress 中允许的内存大小已用尽
- android-studio - Viewpager2 依赖导致启动时崩溃
- typescript - 如何使用 angular8 上传多个文件
? - python - 将“外部”对象添加到集合时运行代码
- selenium - 定位具有特殊标记名和属性的元素 - 量角器
- java - log4j2.xml 文件在 tomcat webapp 中的正确位置
- java - Gson如何排除JsonSerializer中的一些字段
- powershell - Powershell - Add-OdbcDsn - 即使有效也不为驱动程序设置属性
- php - 在 Centos 7 上无法登录 MySQL 服务器(phpMyAdmin)
- laravel - 如何在多维数组中求和?