igniphp/network
Composer 安装命令:
composer require igniphp/network
包简介
PHP swoole based http server
README 文档
README
Requirements
- PHP 7.1 or better
- Swoole extension is required for network server to work
Installation
Linux users:
pecl install swoole
composer install igniphp/network
Mac users with homebrew:
brew install swoole
composer install igniphp/network
or:
brew install homebrew/php/php71-swoole
composer install igniphp/network
Basic Usage
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; // Create server instance. $server = new \Igni\Network\Server(); $server->start();
Listeners
Igni http server uses event-driven model that makes it easy to scale and extend.
There are five type of events available, each of them extends Igni\Network\Server\Listener interface:
Igni\Network\Server\OnStartListenerfired when server startsIgni\Network\Server\OnStopListenerfired when server stopsIgni\Network\Server\OnConnectListenerfired when new client connects to the serverIgni\Network\Server\OnCloseListenerfired when connection with the client is closedIgni\Network\Server\OnRequestListenerfired when new request is dispatched
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; use Igni\Network\Server\Client; use Igni\Network\Server\OnRequestListener; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Igni\Network\Http\Stream; // Create server instance. $server = new \Igni\Network\Server(); // Each request will retrieve 'Hello' response $server->addListener(new class implements OnRequestListener { public function onRequest(Client $client, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { return $response->withBody(Stream::fromString("Hello world")); } }); $server->start();
Configuration
Server can be easily configured with Igni\Network\Server\Configuration class.
Please consider following example:
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; // Listen on localhost at port 80. $configuration = new \Igni\Network\Server\Configuration('0.0.0.0', 80); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
Enabling ssl support
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; $configuration = new \Igni\Network\Server\Configuration(); $configuration->enableSsl($certFile, $keyFile); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
Running server as a daemon
<?php // Autoloader. require_once __DIR__ . '/vendor/autoload.php'; $configuration = new \Igni\Network\Server\Configuration(); $configuration->enableDaemon($pidFile); // Create server instance. $server = new \Igni\Network\Server($configuration); $server->start();
More examples can be found in the ./examples/ directory.
统计信息
- 总下载量: 1.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-13