如何在 Symfony2 中正确使用 webSockets

how to use properly webSockets in Symfony2(如何在 Symfony2 中正确使用 webSockets)
本文介绍了如何在 Symfony2 中正确使用 webSockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在 Symfony2 中实现 websockets,

I'm trying to implement websockets in Symfony2,

我发现这个 http://socketo.me/ 看起来不错.

I found this http://socketo.me/ which seems pretty good.

我在 Symfony 中尝试了它并且它有效,这只是使用 telnet 的一个简单调用.但我不知道如何将其集成到 Symfony 中.

I try it out of Symfony and it works, this was just a simple call using telnet. But I don't know how to integrate this in Symfony.

我想我必须创建一个服务,但我真的不知道哪种服务以及如何从客户端调用它

I think I have to create a service but I don't know realy which kind of service and how to call it from the client

感谢您的帮助.

推荐答案

首先你应该创建一个服务.如果您想注入实体管理器和其他依赖项,请在此处进行.

First you should create a service. If you want to inject your entity manager and other dependencies, do it there.

在 src/MyApp/MyBundle/Resources/config/services.yml 中:

In src/MyApp/MyBundle/Resources/config/services.yml:

services:
    chat:
        class: MyAppMyBundleChat
        arguments: 
            - @doctrine.orm.default_entity_manager

在 src/MyApp/MyBundle/Chat.php 中:

And in src/MyApp/MyBundle/Chat.php:

class Chat implements MessageComponentInterface {
    /**
     * @var DoctrineORMEntityManager
     */
    protected $em;
    /**
     * Constructor
     *
     * @param DoctrineORMEntityManager $em
     */
    public function __construct($em)
    {
        $this->em = $em;
    }
    // onOpen, onMessage, onClose, onError ...

接下来,执行控制台命令来运行服务器.

Next, make a console command to run the server.

在 src/MyApp/MyBundle/Command/ServerCommand.php 中

In src/MyApp/MyBundle/Command/ServerCommand.php

use SymfonyBundleFrameworkBundleCommandContainerAwareCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use RatchetServerIoServer;

class ServerCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('chat:server')
            ->setDescription('Start the Chat server');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $chat = $this->getContainer()->get('chat');
        $server = IoServer::factory($chat, 8080);
        $server->run();
    }
}

现在您有一个带有依赖注入的 Chat 类,您可以将服务器作为控制台命令运行.希望这会有所帮助!

Now you have a Chat class with dependency injections, and you can run the server as a console command. Hope this helps!

这篇关于如何在 Symfony2 中正确使用 webSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)