ekstazi / websocket-server-adapter-amphp
Async WebSocket server adapter for Amp based on different implementations.
1.0
2020-04-10 16:24 UTC
Requires
- php: >=7.2
- ext-pcntl: *
- amphp/amp: ^2.2
- amphp/byte-stream: ^1
- amphp/http-server-router: ^1.0
- amphp/websocket-server: ^2.0
- ekstazi/websocket-common-amphp: ^2.1
- ekstazi/websocket-server-adapter: ^1.0
- psr/log: ^1.1
Requires (Dev)
- amphp/http-client: ^4.2
- amphp/php-cs-fixer-config: dev-master
- amphp/phpunit-util: ^1.1
- amphp/websocket-client: ^1.0@RC
- phpunit/phpunit: ^8 || ^7
Provides
This package is auto-updated.
Last update: 2024-12-11 02:24:56 UTC
README
Adapter of aerys websocket server
Installation
This package can be installed as a Composer dependency.
composer require ekstazi/websocket-server-adapter-amphp
Requirements
PHP 7.2+
Usage
With container
If you have container then add this to your container.php
use Amp\Http\Server\Options; use ekstazi\websocket\server\amphp\FactoryBuilder; use ekstazi\websocket\server\ServerFactory; // .... return [ ServerFactory::class => new FactoryBuilder(), // this is optional config for default options to connections "config" => [ "websocket" => [ 'serverOptions' => new Options(), ] ] ];
Then in your code:
use Amp\Promise; use Amp\Success; use Amp\Socket\Server; use ekstazi\websocket\server\Connection; use ekstazi\websocket\server\ConnectionInfo; use ekstazi\websocket\server\Handler; use ekstazi\websocket\server\ServerFactory; use \Psr\Container\ContainerInterface; /** @var ContainerInterface $container */ /** @var ServerFactory $factory */ $factory = $container->get(ServerFactory::class); $server = $factory->create(Server::listen('tcp://127.0.0.1:8000')); $server->addRoute('/ws', new class() implements Handler { public function onHandshake(ConnectionInfo $connectionInfo): Promise { return new Success(); } public function handle(Connection $connection): Promise { return new Success(); } public function getSubProtocols() : array{ return []; } }); $server->run();
Without container
use Amp\Promise; use Amp\Success; use Amp\Socket\Server; use ekstazi\websocket\server\amphp\ServerFactory; use ekstazi\websocket\server\Connection; use ekstazi\websocket\server\ConnectionInfo; use ekstazi\websocket\server\Handler; /** @var ServerFactory $factory */ $factory = new ServerFactory(new \Amp\Http\Server\Options()); $server = $factory->create(Server::listen('tcp://127.0.0.1:8000')); $server->addRoute('/ws', new class() implements Handler { public function onHandshake(ConnectionInfo $connectionInfo): Promise { return new Success(); } public function handle(Connection $connection): Promise { return new Success(); } public function getSubProtocols() : array{ return []; } }); $server->run();