gpt4 book ai didi

php - 如何在 CakePHP shell 中使用 Ratchet websocket

转载 作者:行者123 更新时间:2023-12-04 16:13:10 24 4
gpt4 key购买 nike

我想创建一个 Websocket 并需要从这个访问 CakePHP ORM。
我正在使用带有以下代码的 Ratchet Websocket:

<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php';

/**
* chat.php
* Send any incoming messages to all connected clients (except sender)
*/
class MyChat implements MessageComponentInterface {
protected $clients;

public function __construct() {
$this->clients = new \SplObjectStorage;
}

public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
}

public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($from != $client) {
$client->send($msg);
}
}
}

public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}

public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}

运行代码:
$app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new MyChat);
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
$app->run();

我需要做两件事:
  • 我需要在 MyChat 类中访问 CakePHP ORM。
  • 我需要在 CakePHP 任务外壳程序中启动该类。

  • 第二个很容易,但第一个我不知道如何初始化 ORM 类以在 MyChat 类中抛出查询。

    最佳答案

    我已经解决了它将自身传递给 MyChat 构造函数的问题,例如:

    $app->route('/chat', new MyChat($this));

    关于php - 如何在 CakePHP shell 中使用 Ratchet websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965582/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com