我打算使用Ratchet和laravel创建即时消息服务(我发现了一个名为latchet的项目,它将Ratchet集成到了laravel中)
我的问题是。
答案 0 :(得分:1)
回答你的第一个问题:
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)
之后,您将从该阵列中获取所有单独的“频道”并将其添加到$subscribedTopics
当您尝试向这些“频道”/“主题”发送内容时,它会将其发送给在$topicArray
我希望这会有所帮助。