地狱,
我尝试使用clankbundle来使用websocket但我无法在我的服务中获取当前用户
在... \ NotificationTopic.php中的非对象上调用成员函数getUser()
我的代码:
-Service.YML
services:
service_Default:
class: Nuwland\WebBundle\Controller\CentreinteretController
arguments: [ @doctrine.orm.entity_manager ]
nuwland.web.notification:
class: Nuwland\WebBundle\Topic\NotificationTopic
arguments: ["@security.context"]
kernel.listener.clank.client_event:
class: Nuwland\WebBundle\EventListener\NuwlandClientEventListener
arguments: ["@security.context"]
tags:
- { name: kernel.event_listener, event: clank.client.connected, method: onClientConnect }
- { name: kernel.event_listener, event: clank.client.disconnected, method: onClientDisconnect }
- { name: kernel.event_listener, event: clank.client.error, method: onClientError }
-NotificationTopic.php
<?php
namespace Nuwland\WebBundle\Services;
use JDare\ClankBundle\Topic\TopicInterface;
use Ratchet\ConnectionInterface as Conn;
use Symfony\Component\Security\Core\SecurityContext;
class NotificationTopic implements TopicInterface
{
private $context;
public function __construct(SecurityContext $securityContext){
$this->context= $securityContext;
}
private function getUser(){
return $this->context->getToken()->getUser();
}
/**
* This will receive any Subscription requests for this topic.
*
* @param \Ratchet\ConnectionInterface $conn
* @param $topic
* @return void
*/
public function onSubscribe(Conn $conn, $topic)
{
$userId = $this->getUser();
var_dump($userId);
}
如果有人可以帮助我, 感谢
答案 0 :(得分:0)
始终检查getToken()是否为null。 用户未在系统中进行身份验证
答案 1 :(得分:0)
您无法通过致电来获取用户:
$this->context->getToken()->getUser()
Websocket服务器对您的应用程序用户一无所知。 在这种情况下 - 您必须插入一个&#39; clientManipulator&#39;服务到您的主题后,您可以
$user = $this->clientManipulator->getClient($connection);
不要忘记检查结果是&#39; instanceof&#39;你的用户对象。并记住,websocket服务器可以访问您的会话cookie(它必须在同一个域中,或者可以访问另一个域名)