如何使用 Ratchet 响应 HTML5 服务器端事件?
How to use Ratchet to respond to HTML5 server-side events?
(注意:我故意在此处放置了不充分的 websocket
标记,因为这是 WebSocket 专家了解 Ratchet 架构的最佳机会)。
我准备实施 HTML5 服务器端事件,我需要的是服务器端解决方案。由于不考虑每个连接挂起 Apache 的一个进程(连接池限制、内存消耗...),我希望 Ratchet 项目 能有所帮助,因为它是维护最多的项目,而且他们http
服务器连同其他组件。
我的问题是:我该如何使用它?不是用于升级 http
请求(默认用法),而是用于提供动态生成的内容。
到目前为止我尝试了什么?
已安装 Ratchet
如教程中所述
已测试 WebSocket 功能 - 正常工作
遵循了一组非常基本的说明 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
class 这里需要声明才能使服务器工作,但是如何声明呢?
Ratchet 文档似乎没有涵盖这一点。
Chat server using Ratchet - Basic
Chat server using Ratchet - Advanced
勾选上面的link。这里的人正在使用 Ratchet 构建一个实时聊天服务器。他基本上是先存储 usernames
,然后再存储 sending/broadcasting。您可以修改它并在发送时检查某些 username
或 uid
是否处于活动状态并仅向它们发送数据。您可以动态生成数据并发送给特定用户或所有用户。也许这会有所帮助。
您的 MyWebPage
class 需要实施 HttpServerInterface
。由于这只是一个简单的 request/response,您需要发送响应,然后在 class:
的 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
class 而不是 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!在您的浏览器中响应。
这就是基本 request/response 系统所需的全部内容,但可以通过实施 HTML 模板和类似的东西进一步扩展它。我自己在 test project 中实现了这个,我已经上传到 github 以及许多其他东西,包括一个抽象控制器,我可以为不同的页面扩展它。
(注意:我故意在此处放置了不充分的 websocket
标记,因为这是 WebSocket 专家了解 Ratchet 架构的最佳机会)。
我准备实施 HTML5 服务器端事件,我需要的是服务器端解决方案。由于不考虑每个连接挂起 Apache 的一个进程(连接池限制、内存消耗...),我希望 Ratchet 项目 能有所帮助,因为它是维护最多的项目,而且他们http
服务器连同其他组件。
我的问题是:我该如何使用它?不是用于升级 http
请求(默认用法),而是用于提供动态生成的内容。
到目前为止我尝试了什么?
已安装
Ratchet
如教程中所述已测试 WebSocket 功能 - 正常工作
遵循了一组非常基本的说明 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
class 这里需要声明才能使服务器工作,但是如何声明呢?
Ratchet 文档似乎没有涵盖这一点。
Chat server using Ratchet - Basic
Chat server using Ratchet - Advanced
勾选上面的link。这里的人正在使用 Ratchet 构建一个实时聊天服务器。他基本上是先存储 usernames
,然后再存储 sending/broadcasting。您可以修改它并在发送时检查某些 username
或 uid
是否处于活动状态并仅向它们发送数据。您可以动态生成数据并发送给特定用户或所有用户。也许这会有所帮助。
您的 MyWebPage
class 需要实施 HttpServerInterface
。由于这只是一个简单的 request/response,您需要发送响应,然后在 class:
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
class 而不是 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!在您的浏览器中响应。
这就是基本 request/response 系统所需的全部内容,但可以通过实施 HTML 模板和类似的东西进一步扩展它。我自己在 test project 中实现了这个,我已经上传到 github 以及许多其他东西,包括一个抽象控制器,我可以为不同的页面扩展它。