【发布时间】:2016-10-21 13:28:34
【问题描述】:
我正在使用这个捆绑包将 Ratchet websocket 集成到我的 Symfony2 项目中:https://github.com/GeniusesOfSymfony/WebSocketBundle
我正在开发一个聊天应用程序。我遇到的问题是如何限制登录用户访问聊天?
websocket 基于 WAMP PubSub 协议。我在 ChatTopic 类中的 subscribe 方法如下所示:
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request) {
$email = $this->clientManipulator->getClient($connection)->getUsername();
$user = $this->userRepository->getByEmail($email);
$msg = array();
$msg["type"] = "userJoined";
$msg["displayName"] = $user->getDisplayName();
$topic->broadcast(['msg' => json_encode($msg)]);
}
如您所见,我设法在我的 websocket 中获取用户会话并从数据库中获取所有用户数据。 我只是不知道如何防止未经授权的用户订阅聊天。
【问题讨论】:
-
我认为您可以使用
$connection->close()关闭该用户的连接。
标签: php symfony websocket ratchet