PHP code example of ekstazi / websocket-server-adapter-amphp

1. Go to this page and download the library: Download ekstazi/websocket-server-adapter-amphp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ekstazi / websocket-server-adapter-amphp example snippets


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(),
        ]
    ]
];


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();

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();