cydrickn / thruway-swoole-transport
Swoole Transport for Thruway
v1.1.1
2023-11-06 11:27 UTC
Requires
- php: >=8.1
- ext-openswoole: *
Requires (Dev)
- mockery/mockery: ^1.6
- openswoole/ide-helper: ^22.0
- phpunit/phpunit: ^10.3
- voryx/thruway: ^0.6.1
README
OpenSwoole / Swoole Websocket Transport for Thruway Router
Prerequisite:
- PHP version >= 8.1
- OpenSwoole
- voryx/thruway
Installation
composer require cydrickn/thruway-swoole-transport
Example
Ouside the worker start
<?php require_once __DIR__ . '/vendor/autoload.php'; use Thruway\SwooleTransport\Server\WebsocketServer; use Thruway\SwooleTransport\SwooleLoop; use Thruway\SwooleTransport\SwooleTransportProvider; use Thruway\Peer\Router; $server = new WebsocketServer('127.0.0.1', 9999); $transportProvider = new SwooleTransportProvider($server); $router = new Router(new SwooleLoop()); $router->addTransportProvider($transportProvider); $server->addListener(WebsocketServer::EVENT_SERVER_START, function () use($transportProvider, $server) { \Thruway\Logging\Logger::info($transportProvider, 'Websocket listening on 0.0.0.0:9000'); $server->shutdown(); }); $router->start(false);
Inside Worker Start
<?php require_once __DIR__ . '/vendor/autoload.php'; use Thruway\SwooleTransport\Server\WebsocketServer; use Thruway\SwooleTransport\SwooleLoop; use Thruway\SwooleTransport\SwooleTransportProvider; use Thruway\Peer\Router; $server = new WebsocketServer('127.0.0.1', 9999); $server->addListener(WebsocketServer::EVENT_SERVER_START, function () { \Thruway\Logging\Logger::info(null, 'Websocket listening on 0.0.0.0:9000'); }); $server->addListener(WebsocketServer::EVENT_WORKER_START, function () use($server) { $transportProvider = new SwooleTransportProvider($server); $router = new Router(new SwooleLoop()); $router->addTransportProvider($transportProvider); $router->start(false); });