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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿