Symfony 2 GeniusesOfSymfony / WebSocketBundle

时间:2015-07-16 12:27:06

标签: symfony websocket

我在symfony 2应用程序中工作,我需要使用websocket。

我找到了一个名为GeniusesOfSymfony / WebSocketBundle的软件包,并将其集成到系统中。此捆绑包基于JDare / ClankBundle,但是firth具有TopicPeriodicTimerInterface用于每隔定义的时间重新发送客户端的信息。

我在我的应用程序中有它,但我需要获取已登录的用户。该捆绑包有一个名为@ gos_web_socket.websocket.client_manipulator的服务来操纵用户信息,但是当我尝试获取用户信息时,只有该服务才能获得匿名用户,但我已经为某个用户登录了。

任何人有同样的问题或知道解决方案???

2 个答案:

答案 0 :(得分:2)

我邀请您按照会话共享和用户身份验证的步骤进行操作,如您使用的捆绑包的文档中所述:https://github.com/GeniusesOfSymfony/WebSocketBundle/blob/master/Resources/docs/SessionSetup.md

首先你需要实现一个Symfony会话处理程序,如果你决定使用PDO会话处理程序,那么doc就在这里:http://symfony.com/doc/master/cookbook/configuration/pdo_session_storage.html(如果你选择的话,不要忘记创建相应的数据库,声明所有的服务,参数等)。

接下来,您必须将config.yml设置为使用会话处理程序:

framework:
    ...
    session:
        handler_id: session.handler.pdo # adapt if you chose a different one

设置GOS Websocket也可以使用它:

gos_web_socket:
    ...
    client:
        firewall: main # the name of your firewall (can be an array if multiple)
        session_handler: @session.handler.pdo

第一个链接提供的文档末尾将向您展示使用客户端操纵器的一些方法。我知道复制粘贴更好,但我真的不觉得复制粘贴整个文档是有用的,也不清楚。

对于我自己的用途,我没有客户端操纵器,我只是使用

$this->clientStorage->getClient($connection->WAMP->clientStorageId);

以便使用当前连接检索用户。如果您将(clientStorage)作为参数提供给服务构造函数,则@gos_web_socket.client_storage可用。当然,您需要调整构造函数:

class AcmeTopic implements TopicInterface
{
    /**
     * @param ClientStorageInterface $clientStorage
     */
    protected $clientStorage;

    public function __construct(ClientStorageInterface $clientStorage)
    {
        ...

要访问其他用户,您可以从以下方面获取灵感:

foreach($topic as $subscriber)
{
    $subscriber->event($topic->getId(),
                    ['msg' => $this->clientStorage
                                   ->getClient($connection->WAMP->clientStorageId)
                                   ->getUsername().' is now online!']);
}

希望这有帮助,我没有那么多经验,因为这也是我第一次使用它。我邀请你直接在GitHub上询问你是否仍然卡住(问题部分),因为作者可能会帮助你更多!

(另外,我假设你使用主题)

答案 1 :(得分:1)

我有类似的情况并获得了登录用户。 如果您按照所有配置步骤操作,现在尝试访问您的应用程序的IP(127.0.0.1),例如:http://127.0.0.1/app_dev.php/chat/ 我曾经使用我的域名(http://mydominename/app_dev.php/chat/)进行访问,而且我没有在GeniusesOfSymfony / WebSocketBundle中获取已登录的用户。

我的代码:

use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;    
class ChatTopic implements TopicInterface
{
    protected $clientManipulator;

/**
 * @param ClientManipulatorInterface $clientManipulator
 */
public function __construct(ClientManipulatorInterface $clientManipulator)
{
    $this->clientManipulator = $clientManipulator;
}

public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
    var_dump($this->clientManipulator->getClient($connection)); //To show in WebSocket console.
    /** Rest of code **/
}

不要忘记向服务添加新参数

myclass_name.chat_topic:
    class: MyBundle\Topic\ChatTopic
    tags:
        - { name: gos_web_socket.topic }
        arguments: [ @gos_web_socket.websocket.client_manipulator ]

这可以解决您的问题。

相关问题