dongshenling6585 2015-12-28 15:34
浏览 346
已采纳

如何使用Ratchet响应HTML5服务器端事件?

(Note: I've intentionally put non adequate websocket tag here, as it's best chance for WebSocket expert folks to know architecture of Ratchet).

I'm up for implementing HTML5 server side events, and what I need is server side solution. Since hanging Apache's one process per connection (connection pool limit, memory consumption...) is out of consideration I was hoping that Ratchet project can be of help, since it's most maintained project and they have http server coupled along with other components.

My question is: how can I use it? Not for upgrading http request (default usage), but for serving dynamically generated content.

What have I tried so far?

  1. installed Ratchet as explained in tutorial

  2. tested WebSocket functionality - works properly

  3. followed very basic set of instructions 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();

One should not be an expert to figure out that MyWebPage class here needs to be declared in order for server to work, but how?

The Ratchet documentation does not seems to cover this.

  • 写回答

2条回答 默认 最新

  • dongxuan1660 2016-07-31 16:43
    关注

    Your MyWebPage class needs to implement HttpServerInterface. Since it's just going to be a simple request/response you need to send a response and then close the connection within the onOpen() method of your class:

    <?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();
        }
    }
    

    I ended up using the Ratchet\App class instead of Ratchet\Http\HttpServer because it allows you to set up routing among other things, so your /bin/http-server.php would then look like this:

    <?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();
    

    When you run php bin/http-server.php and visit http://localhost:8080 you should see the Hello World! response in your browser.

    This is all you need for a basic request/response system, but it could be extended further by implementing HTML templates and things like that. I've implemented this myself in a little test project which I've uploaded to github along with a lot of other things, including an abstract controller which I can extend for different pages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)