我正在使用此捆绑将棘轮websocket整合到我的Symfony2项目中:https://github.com/GeniusesOfSymfony/WebSocketBundle如何从PHP Ratchet WebSocket中拒绝未经过身份验证的用户?
我正在开发一个聊天应用程序。我遇到的问题是如何限制对登录用户的聊天访问?
websocket基于WAMP PubSub协议。我在ChatTopic类认购方法如下:
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()'关闭该用户的连接。 – qooplmao