棘轮 laravel

Ratchet with laravel

我正计划使用 Ratchet 和 laravel(我找到了一个名为 latchet 的项目,它将 Ratchet 集成到 laravel)

我的问题是

  1. 如何在允许客户端启动网络套接字连接之前验证客户端。
  2. 一个用户可以是多个组的一部分,所以我想创建像 Poll_grp_id 这样的频道,并获取一个用户所有组的列表,然后订阅所有这些 Poll_Grp_id 频道.我不确定该怎么做

回答你的第一个问题:

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)

然后你从那个数组中得到所有单独的 'channels' 并将它们添加到 $subscribedTopics

当您尝试向 'channels'/'topics' 发送内容时,它会将其发送给所有已连接的客户端,这些客户端在其 $topicArray[=16= 中具有特定频道]

希望对您有所帮助。