Symfony 2 GeniusesOfSymfony/WebSocketBundle

大兔子大兔子 提交于 2019-12-04 19:24:32

问题


I'm working in a symfony 2 application and I need to use websocket.

I found a bundle named GeniusesOfSymfony/WebSocketBundle and I integrate it in the system. This bundle is based on JDare/ClankBundle but the firth has the TopicPeriodicTimerInterface using for resend the information for the client every a defined time.

I have it in my application but I need to get the logged user. The bundle has a service named @gos_web_socket.websocket.client_manipulator to manipulate the user information but when I try to get user information only the service get me the anonymous user but I'm logged for someone user.

Any had the same problem or know a solution for that???


回答1:


I invite you to follow the procedure for session sharing and user authentication, as explained in the doc of the bundle you use: https://github.com/GeniusesOfSymfony/WebSocketBundle/blob/master/Resources/docs/SessionSetup.md

First you need to implement a Symfony session handler, if you decide to use the PDO Session Handler, the doc is here: http://symfony.com/doc/master/cookbook/configuration/pdo_session_storage.html (Do not forget to create the corresponding DB if you chose so, declaring all the services, parameters, and so on).

Next, you have to set your config.yml to use your session handler:

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

Setup GOS Websocket for using it as well:

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

The end of the documentation available at the first link will show you some ways of using your client manipulator. I know it is better to copy paste, but I really don't feel like copy pasting the entire doc is useful, nor clear.

For my own use, I do not have a client manipulator, I simply use

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

in order to retrieve the user with the current connection. The clientStorage is available if you give it (@gos_web_socket.client_storage) to the service constructor as argument. Of course, you need to adapt your constructor:

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

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

To access other users, you can take some inspiration from:

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

Hope this helps, I do not have that much experience with it, as it is my first use as well. I invite you to ask directly on the GitHub if you are still stuck (issues part), as the author can probably help you more!

(Also, I assumed you use the Topic)




回答2:


I had a similar situation and got the logged in user. If you follow all the configuration steps, now tries to access the IP of your application (127.0.0.1) ex: http://127.0.0.1/app_dev.php/chat/ I used to acces with my domain name (http://mydominename/app_dev.php/chat/) and and I did't get the logged user in GeniusesOfSymfony/WebSocketBundle.

My code:

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 **/
}

Do not forget to add a new argument to the service

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

This can be a start to your solution.



来源:https://stackoverflow.com/questions/31454122/symfony-2-geniusesofsymfony-websocketbundle

工具导航Map