gplanchat/php-io

该软件包最新版本(dev-master)没有提供许可证信息。

一个异步IO库,使用chobie的php-uv扩展,用于构建网络守护进程

维护者

详细信息

github.com/gplanchat/php-io

源代码

dev-master 2014-06-22 18:09 UTC

This package is auto-updated.

Last update: 2024-08-29 03:52:44 UTC


README

php-io 是一个面向对象的事件驱动输入输出库,主要用于为PHP脚本提供网络流量。

这个库部分基于 php-uvlibuv 绑定,但也计划添加其他适配器(pollig, libev, libevent)

HTTP/WebSocket服务器示例

可以按照这种方式实现一个简单的HTTP服务器

<?php

use Gplanchat\Io\Adapter\Libuv\DefaultServiceManager as LibuvServiceManager;
use Gplanchat\Io\Application\Application;
use Gplanchat\Io\Net\Protocol\Http;
use Gplanchat\EventManager\Event;
use Gplanchat\Io\Net\Tcp\ClientInterface;
use Gplanchat\Io\Net\Tcp\Plugin\Server as TcpServerPlugin;
use Gplanchat\Io\Net\Protocol\Http\Plugin\Server as HttpServerPlugin;


$httpListener = function(Event $event, ClientInterface $client, Http\Request $request, Http\Response $response) {
    $htmlBody = <<<HTML_EOF
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello world</h1>
  </body>
</html>
HTML_EOF;

    $response
        ->setHeader('Content-Type', 'text/html')
        ->setBody($htmlBody)
        ->setReturnCode(200, 'OK')
        ->emit(new Event('ready'))
    ;
};

(new Application(new LibuvServiceManager()))
    ->registerPlugin(new TcpServerPlugin(), 'TcpServer', 0)
    ->registerPlugin(new HttpServerPlugin($httpListener), 'HttpServer', 0)
    ->init(function(Event $event, Application $application) {
        $application->callPlugin('TcpServer', ['0.0.0.0', 8081]);
        $application->callPlugin('HttpServer', ['TcpServer', 200]);
    })
    ->bootstrap()
    ->run()
;

通过调用 init() 方法添加WebSocket支持

    ->init(function(Event $event, Application $application) {
        /*
         * Adding WebSocket (RFC 6455) support
         */
        $webSocketServiceManager = new WebSocket\ServerServiceManager();

        $webSocketListener = function(Event $event, ClientInterface $client, WebSocket\Request $request, WebSocket\Response $response) {
            $response
                ->addMessage(['Date' => date('c'), 'Hello' => 'World'])
                ->emit(new Event('ready'))
            ;
        };

        $httpServer = $application->getStorage('HttpServer');
        $httpServer->registerPlugin('WebSocket', new Http\Plugin\WebSocket($webSocketServiceManager, $webSocketListener));
    })

使用数据库连接

目前,仅支持作为基本功能使用mysqli和mysqlnd的MySQL。

这是实现MySQL连接处理程序的方法

use Gplanchat\Io\Application\Application;
use Gplanchat\EventManager\Event;
use Gplanchat\Io\Adapter\Libuv\DefaultServiceManager as LibuvServiceManager;
use Gplanchat\Io\Db\Mysql\DefaultServiceManager as MysqlServiceManager;

(new Application(new LibuvServiceManager()))
    ->init(function(Event $event, Application $application) {
        // Initiating the poller and assigning it to the main loop through a timer
        /** @var \Gplanchat\Io\Loop\TimerInterface $timer */
        $timer = $application->getServiceManager()->get('Timer', [$application->getCurrentLoop()]);

        $serviceManager = new MysqlServiceManager();
        $poller = $serviceManager->get('Poller');

        $link = $serviceManager->get('Connection', ['localhost', 'root', '', 'foo_database']);
        $poller->addConnection($link);

        $timer->interval(1, $poller);
        $application->setStorage('Db', $link);
    })
    ->init(function(Event $event, Application $application) {
        // Sending requests to the RDBMS
        $worker = $application->getServiceManager()->get('Timer', [$application->getCurrentLoop()]);
        $worker->interval(20, function() use($application) {
            $link = $application->getStorage('Db');

            $link->query('SELECT * FROM foo_table ORDER BY RAND() LIMIT 1', function($result) {
                var_dump($result);
            });
        });
    })
    ->bootstrap()
    ->run()
;

文档

API文档是用php-docgen构建的。 » 阅读文档