我一直在关注socketo.me ratchet网站上的指示,到目前为止一直都很好。我有兴趣向只订阅它的用户广播主题,而不是每个人。
下面给出的代码以及here on their website令我感到困惑。
<?php
use Ratchet\ConnectionInterface as Conn;
/**
* When a user publishes to a topic all clients who have subscribed
* to that topic will receive the message/event from the publisher
*/
class BasicPubSub implements Ratchet\Wamp\WampServerInterface {
public function onPublish(Conn $conn, $topic, $event, array $exclude, array $eligible) {
$topic->broadcast($event);
}
public function onCall(Conn $conn, $id, $topic, array $params) {
$conn->callError($id, $topic, 'RPC not supported on this demo');
}
// No need to anything, since WampServer adds and removes subscribers to Topics automatically
public function onSubscribe(Conn $conn, $topic) {}
public function onUnSubscribe(Conn $conn, $topic) {}
public function onOpen(Conn $conn) {}
public function onClose(Conn $conn) {}
public function onError(Conn $conn, \Exception $e) {}
}
以下信息在他们的网站上我解释说它应该与public function onPublish
集成。
此组件触发的事件:
onPublish(ConnectionInterface $ conn,Topic $ topic,string $ event) - 用户将数据发布到$ topic。您应该返回已订阅$ topic
的连接的事件命令WAMP:
(string $ sessionId) - 给客户端的唯一ID
(array $ subscriptions) - 客户订阅的主题集合
他们不应该像这个
这样的代码class BasicPubSub implements Ratchet\Wamp\WampServerInterface {
public function onPublish(Conn $conn, $topic, $event, $sessionid, array $subscriptions, array $exclude, array $eligible) {
$topic->broadcast($subscriptions);
}
/** rest of code here */
}
显然$subscriptions
将填充sql query
以根据$sessionid
检索用户订阅的主题,$topic->broadcast($subscriptions);
会将主题广播给各个用户认购。我没有看到教程在做什么,显然我的解释是不同的。我需要帮助!!!