【问题标题】:How to use Ratchet to respond to HTML5 server-side events?如何使用 Ratchet 响应 HTML5 服务器端事件?
【发布时间】:2015-12-28 15:34:52
【问题描述】:

(注意:我故意在此处放置了不够充分的websocket 标签,因为这是让 WebSocket 专家了解 Ratchet 架构的最佳机会)。

我准备实现 HTML5 服务器端事件,我需要的是服务器端解决方案。由于不考虑每个连接挂起 Apache 的一个进程(连接池限制、内存消耗......),我希望 Ratchet 项目 能有所帮助,因为它是维护最多的项目,他们有 @987654323 @server 与其他组件耦合在一起。

我的问题是:我该如何使用它?不是为了升级 http 请求(默认使用),而是为了提供动态生成的内容。

到目前为止我尝试了什么?

  1. 按照教程中的说明安装Ratchet

  2. 经过测试的 WebSocket 功能 - 工作正常

  3. 遵循非常基本的指令集given on page that describes http server component

/bin/http-server.php

use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
    require dirname(__DIR__) . '/vendor/autoload.php';
    $http = new HttpServer(new MyWebPage);

$server = IoServer::factory($http);
$server->run();

专家不应该知道这里的MyWebPage 类需要声明才能使服务器工作,但是如何?

Ratchet 文档似乎没有涵盖这一点。

【问题讨论】:

  • 你有没有得到任何地方?
  • @Andy 很遗憾没有。
  • 我昨天玩了一整天,现在有一个基本的工作 HTTP 服务器。等我整理好一切后,我会在接下来的几天内发布答案。

标签: php websocket ratchet


【解决方案1】:

您的MyWebPage 类需要实现HttpServerInterface。由于它只是一个简单的请求/响应,您需要发送响应,然后在您的类的 onOpen() 方法中关闭连接:

<?php

use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;

class MyWebPage implements HttpServerInterface
{
    protected $response;

    public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
    {
        $this->response = new Response(200, [
            'Content-Type' => 'text/html; charset=utf-8',
        ]);

        $this->response->setBody('Hello World!');

        $this->close($conn);
    }

    public function onClose(ConnectionInterface $conn)
    {
    }

    public function onError(ConnectionInterface $conn, \Exception $e)
    {
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
    }

    protected function close(ConnectionInterface $conn)
    {
        $conn->send($this->response);
        $conn->close();
    }
}

我最终使用了Ratchet\App 类而不是Ratchet\Http\HttpServer,因为它允许您设置路由等,因此您的/bin/http-server.php 将如下所示:

<?php

use Ratchet\App;

require dirname(__DIR__) . '/vendor/autoload.php';

$app = new App('localhost', 8080, '127.0.0.1');
$app->route('/', new MyWebPage(), ['*']);

$app->run();

当您运行php bin/http-server.php 并访问http://localhost:8080 时,您应该会看到Hello World!在您的浏览器中响应。

这就是一个基本的请求/响应系统所需要的一切,但它可以通过实现 HTML 模板和类似的东西来进一步扩展。我自己在 test project 中实现了这一点,我已经将它与许多其他东西一起上传到了 github,包括我可以为不同页面扩展的抽象控制器。

【讨论】:

  • 似乎正是我正在寻找的 - 谢谢你的努力。我会在测试您的代码后立即接受您的回答 - 到目前为止投票。
  • @Miloshio Github 项目也包含比这个答案更多的功能 - 它可以提供 HTML 模板,而不仅仅是基本的纯文本响应。还有一些其他的东西,比如用于获取和设置配置值的配置对象,还有一个记录器 :) HomeController 展示了如何使用请求对象。
  • 是的,这是功能齐全的 http 服务器。现在由我来执行Content-Type: text/event-stream 和脉冲更新。非常感谢。我会在 Github 上 fork 你的项目。
【解决方案2】:

Chat server using Ratchet - Basic

Chat server using Ratchet - Advanced

检查上面的链接。这里的人正在使用 Ratchet 构建一个实时聊天服务器。他最初基本上是存储usernames,然后向所有人发送/广播。您可以修改它并在发送时检查某些usernameuid 目前是否处于活动状态并仅向它们发送数据。您可以动态生成数据并发送给特定用户或所有用户。可能这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多