Symfony 2 GeniusesOfSymfony/WebSocketBundle [英] Symfony 2 GeniusesOfSymfony/WebSocketBundle
问题描述
我在 symfony 2 应用程序中工作,我需要使用 websocket.
I'm working in a symfony 2 application and I need to use websocket.
我找到了一个名为 GeniusesOfSymfony/WebSocketBundle 的包,我将它集成到系统中.该包基于 JDare/ClankBundle,但第一个包具有 TopicPeriodicTimerInterface,用于每隔定义的时间为客户端重新发送信息.
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.
我的应用程序中有它,但我需要获取登录的用户.该捆绑包有一个名为 @gos_web_socket.websocket.client_manipulator 的服务来操作用户信息,但是当我尝试获取用户信息时,只有该服务才能获取匿名用户,但我已为某个用户登录.
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???
推荐答案
我邀请您遵循会话共享和用户身份验证的过程,如您使用的包的文档中所述:https://github.com/GeniusesOfSymfony/WebSocketBundle/blob/master/Resources/docs/SessionSetup.md
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
首先你需要实现一个Symfony session handler,如果你决定使用PDO Session Handler,文档在这里:http://symfony.com/doc/master/cookbook/configuration/pdo_session_storage.html(如果你选择这样做,不要忘记创建相应的数据库,声明所有服务、参数等).
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).
接下来,您必须设置 config.yml 以使用会话处理程序:
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
设置 GOS Websocket 以使用它:
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);
为了检索具有当前连接的用户.clientStorage
如果您将它 (@gos_web_socket.client_storage
) 作为参数提供给服务构造函数,则它可用.当然,你需要调整你的构造函数:
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!']);
}
希望这会有所帮助,我对它没有太多经验,因为它也是我第一次使用.有没有卡住(issues部分),请直接在GitHub上提问,作者或许能帮到你更多!
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)
这篇关于Symfony 2 GeniusesOfSymfony/WebSocketBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!