gpt4 book ai didi

php - Ratchet 与 Laravel

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

我正在计划使用Ratchet和laravel创建一个即时通讯服务(我找到了一个名为latchet的项目,它将Ratchet集成到laravel中)

我的问题是。

  • 如何在允许客户端启动 Web 套接字连接之前对其进行验证。
  • 一个用户可以属于多个组,所以我想创建像 Poll_grp_id 这样的 channel ,并获取所有用户组的列表,然后订阅所有这些 Poll_Grp_id channel 。我不知道如何去做
  • 最佳答案

    回答你的第一个问题:

    public function onSubscribe(ConnectionInterface $conn, $topic, $userKey) {
    //check if the userKey is correct for that user, ...
    }

    我自己将它与 APIKey 一起使用,每次用户订阅时,他们都会发送 $userKey 对象。

    该对象仅包含 userId、apiKey、...(验证用户所需的任何内容)

    第二个问题:

    //来自socketo.me的代码举例
    protected $subscribedTopics = array();

    public function onSubscribe(ConnectionInterface $conn, $topic) {
    $this->subscribedTopics[$topic->getId()] = $topic;
    }

    /**
    * @param string JSON'ified string we'll receive from ZeroMQ
    */
    public function onBlogEntry($entry) {
    $entryData = json_decode($entry, true);

    // If the lookup topic object isn't set there is no one to publish to
    if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
    return;
    }

    $topic = $this->subscribedTopics[$entryData['category']];

    // re-send the data to all the clients subscribed to that category
    $topic->broadcast($entryData);
    }

    在这种情况下,您将更改 onSubscribe($conn, $topic)onSubscribe($conn, $topicArray)
    之后,您从该数组中获取所有单独的“ channel ”并将它们添加到 $subscribedTopics
    当您尝试向那些“ channel ”/“主题”发送内容时,它会将其发送到所有在其 $topicArray 中具有特定 channel 的已连接客户端。

    我希望这有帮助。

    关于php - Ratchet 与 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37462391/

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