Closed eleimt closed 3 years ago
Please do what the exception message says and inspect the reasons via getReasons().
@kelunik
I edit code and get: Fatal error: Uncaught Amp\MultiReasonException: Multiple errors encountered; use Amp\MultiReasonException::getReason s() to retrieve the array of exceptions thrown in
My code:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Amp\Websocket\Client\Connection;
use Amp\Websocket\Message;
use Amp\MultiReasonException;
use function Amp\delay;
use function Amp\Websocket\Client\connect;
Amp\Loop::run(function () {
try {
/** @var Connection $connection */
$connection = yield connect('ws://demos.kaazing.com/echo');
yield $connection->send("Hello!");
$i = 0;
while ($message = yield $connection->receive()) {
/** @var Message $message */
$payload = yield $message->buffer();
printf("Received: %s\n", $payload);
if ($payload === "Goodbye!") {
$connection->close();
break;
}
yield delay(1000); // Pause the coroutine for 1 second.
if ($i < 3) {
yield $connection->send("Ping: " . ++$i);
} else {
yield $connection->send("Goodbye!");
}
}
} catch (MultiReasonException $exception) {
print_r($exception->getReasons());
}
});
Am I using Amp\MultiReasonException correctly
?
Try putting the try / catch around the Loop::run in that case.
@kelunik Without changes
Exist solutions?
I'm not sure why you can't catch that exception locally. I get the following exception if I try to run your example:
PHP Fatal error: Uncaught Amp\Websocket\Client\ConnectionException: A Switching Protocols (101) response was not received; instead received response status: OK (200) in C:\Users\Niklas\Documents\GitHub\websocket-client\src\Rfc6455Connector.php:83
The demo in the examples
directory uses $connection = yield connect('wss://echo.websocket.org');
instead, so maybe try that? That one works fine for me.
@kelunik Yes, with this example I get the answer:
Received: Hello!
Received: Ping: 1
Received: Ping: 2
Received: Ping: 3
Received: Goodbye!
How do I make a connection by passing the Authorization Bearer
?
I try like this:
$handshake = new Handshake('wss://{my_host}');
$handshake->withHeader('Authorization', 'Bearer {key}');
/** @var Connection $connection */
$connection = yield connect($handshake);
But I get the error:
Fatal error: Uncaught Amp\Websocket\Client\ConnectionException: A Switching Prot
ocols (101) response was not received; instead received response status: Unautho
rized (401) in amp\vendor\amphp\websocket-client\src\Rfc64
55Connector.php:83
withHeader
returns a new object, so you'll have to use $handshake = $handshake->withHeader(...)
.
Okay, it's work. Thanks! @kelunik
Hello, I try connected by example from README, but get exception:
Fatal error: Uncaught Amp\MultiReasonException: Multiple errors encountered; use Amp\MultiReasonException::getReason s() to retrieve the array of exceptions thrown in
My code: