entity-framework - 如何在 websocket symfony 应用程序的服务中使用实体、表单、 Controller

标签 entity-framework symfony doctrine-orm websocket ratchet

我正在 Symfony2 中开发一个 websocket 应用程序。我使用基于 Ratchet ( https://github.com/JDare/ClankChatBundle ) 的 symfony2 包调用 ClankBundle ( http://socketo.me/ )。

我已经成功地在 symfony2 中配置了我的服务并且服务器正在工作......
例如,当我调用 JS network.onSubscribe 时,每个已经订阅的人都会收到信息。

class ChatTopic implements TopicHandlerInterface
{
/**
 * Announce to this topic that someone else has joined the chat room
 * Also, set their nickname to Guest if it doesnt exist.
 * 
 * @param \Ratchet\ConnectionInterface $conn
 * @param $topic
 */
  public function onSubscribe(Conn $conn, $topic)
  {
      if (!isset($conn->ChatNickname))
      {
          $conn->ChatNickname = "Guest"; **how i have to do if i want to use "$this->getUser(); " here ?**
      }

      $msg = $conn->ChatNickname . " joined the chat room.";

      $topic->broadcast(array("msg" => $msg, "from" => "System"));
  }

但是现在,我想使用我已经构建的一些其他工具,例如“在我的服务中”的某些实体或表单。

例如,我希望能够在我的服务中执行“$this->getUser()”以返回用户的伪代码。例如,向连接到该 channel 的每个客户端返回“伪已加入此 channel ”。

这个类(class)是我服务的一部分,我想在里面使用
$this->getUser 

或者
$em = $this->getDoctrine()->getManager();
$em->persist($music);"

.

或者我想坚持发送在 Doctrine 中抛出我的 websocket 的东西。 (比如保存连接到 websocket channel 的任何人发送的每条消息。

就像你看到的,我对 Symfony2 和 websocket 不太满意,但我正在学习!

我希望我很清楚(对不起我的英语......)并且有人可以帮助我!谢谢。

最佳答案

要持久化实体,您需要首先将实体管理器注入(inject)您的类。

class ChatTopic implements TopicHandlerInterface
{
    protected $em;
    public function __construct($em) {
        $this->em = $em;
    }
}

您需要在 services.xml 中注入(inject)依赖项
<services>
    <service id="jdare_clank.chat_topic_handler" class="JDare\ClankChatBundle\Topic\ChatTopic">
        <argument>"@doctrine.orm.default_entity_manager"</argument>
    </service>

并从 Controller 或其他一些 ContainerAwareInterface 中的服务容器中获取您的类:
$chatTopic = $this->getContainer()->get('jdare_clank.chat_topic_handler');

获取用户比较棘手,因为您无法访问该类中的安全上下文,因为它不知道容器。有几种方法可以做到。在我们的例子中,我们实际上实现了一个安全的 websocket (wss) 协议(protocol)并在其中创建了一个登录协议(protocol),所以我们可以为每个连接存储一个用户 ID。但是一种快速而肮脏的方法是简单地将用户 ID 添加到另一个 Controller 中的 session 中。
$userId = $this->get('security.context')->getToken()->getUser()->getId();
$session = $this->get('session');
$session->set('user', (str) $userId);

然后,您可以从类(class)内的 session 中获取用户。
public function onSubscribe(Conn $conn, $topic)
{
    $userId = $conn->Session->get('user');
    $user = $this->em->getRepository('AcmeUserBundle:User')->find((int) $userId);
    ...

希望这是有帮助的。如果其中任何一个让您失望,请告诉我,我会尽力提供帮助。依赖注入(inject)有点难以理解,但它是您工具包中的一个非常强大的工具!

关于entity-framework - 如何在 websocket symfony 应用程序的服务中使用实体、表单、 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17949025/

相关文章:

javascript - 如何将 Backbone.js 与 Symfony 框架和/或 Apache Thrift 一起使用

c# - Entity Framework 结合来自不同实体的 lambda 表达式

c# - SQL 数据库中一对多关系的性能问题

javascript - Assetic javascripts 从非空源(包内)创建空文件

Symfony+JMS Serializer反序列化为现有对象

Symfony preUpdate 与 prePersist

doctrine-orm - Doctrine 2 按 ASC 排序,最后为 Null 值

c# - 字符串 EndsWith 方法中 Entity Framework Linq 中的奇怪行为

sql - Entity Framework 连接查询与 int 数组

symfony - 如何使用 symfony2 生成 pdf 报告?