[英][Symfony2]current user in service
地獄,
我嘗試使用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);
}
如果有人可以幫助我,謝謝
始終檢查getToken()是否為null。 用戶未在系統中認證
您無法通過以下方式獲得用戶:
$this->context->getToken()->getUser()
Websocket服務器對您的應用程序用戶一無所知。 在這種情況下,您必須在主題中插入“ clientManipulator”服務,之后您可以
$user = $this->clientManipulator->getClient($connection);
不要忘記檢查結果是否是您的User對象的“實例”。 並且請記住,websocket服務器可以訪問您的會話cookie(它必須在同一域中,或者可以訪問另一個域名)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.