ekstazi/websocket-stream-client-amphp
is ekstazi/websocket-stream-client
implementation based on amphp/websocket-client
This package can be installed as a Composer dependency.
composer require ekstazi/websocket-stream-client-amphp
PHP 7.2+
If you have container then add this to your container.php
use Amp\Websocket\Options;
use \ekstazi\websocket\client\amphp\ConnectorFactory;
use \ekstazi\websocket\client\ConnectionFactory;
// ....
return [
ConnectionFactory::class => new ConnectorFactory(),
// this is optional config for default options to connections
"config" => [
"websocket" => [
'clientOptions' => Options::createClientDefault(),
]
]
];
Then in your code:
use \Psr\Container\ContainerInterface;
use \ekstazi\websocket\client\ConnectionFactory;
use \Psr\Http\Message\RequestInterface;
use \ekstazi\websocket\client\Connection;
/** @var ContainerInterface $container */
/** @var ConnectionFactory $connector */
/** @var RequestInterface $request */
$connector = $container->get(ConnectionFactory::class);
/** @var Connection $stream */
$stream = yield $connector->connect($request, Connection::MODE_BINARY);
You can use functions to do the same:
use \Psr\Http\Message\RequestInterface;
use \ekstazi\websocket\client\Connection;
use function \ekstazi\websocket\client\connect;
/** @var RequestInterface $request */
/** @var Connection $stream */
$stream = yield connect($request, Connection::MODE_BINARY);
or
use \ekstazi\websocket\client\ConnectionFactory;
use \Psr\Http\Message\RequestInterface;
use \ekstazi\websocket\client\Connection;
use function \ekstazi\websocket\client\connector;
/** @var RequestInterface $request */
/** @var ConnectionFactory $connector */
$connector = connector();
/** @var Connection $stream */
$stream = yield $connector->connect($request, Connection::MODE_BINARY);
use Amp\Websocket\Options;
use \ekstazi\websocket\client\ConnectionFactory;
use \Psr\Http\Message\RequestInterface;
use \ekstazi\websocket\client\Connection;
use function \ekstazi\websocket\client\connector;
/** @var RequestInterface $request */
/** @var ConnectionFactory $connector */
$connector = connector();
/** @var Connection $stream */
$stream = yield $connector->connect($request, Connection::MODE_BINARY, Options::createClientDefault()->withoutHeartbeat());
The default options can be overridden as shown in above example
use Amp\Websocket\Options;
use ekstazi\websocket\client\amphp\Connector;
use \ekstazi\websocket\client\ConnectionFactory;
use \Psr\Http\Message\RequestInterface;
use \ekstazi\websocket\client\Connection;
/** @var RequestInterface $request */
/** @var ConnectionFactory $connector */
$connector = new Connector(null, Options::createClientDefault());
/** @var Connection $stream */
$stream = yield $connector->connect($request, Connection::MODE_BINARY);