amphp / websocket-client

Async WebSocket client for PHP based on Amp.
https://amphp.org/websocket-client
MIT License
147 stars 17 forks source link

It's not working #34

Closed eleimt closed 3 years ago

eleimt commented 3 years ago

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:

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Amp\Websocket\Client\Connection;
use Amp\Websocket\Message;
use function Amp\delay;
use function Amp\Websocket\Client\connect;

Amp\Loop::run(function () {
    /** @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!");
        }
    }
});
kelunik commented 3 years ago

Please do what the exception message says and inspect the reasons via getReasons().

eleimt commented 3 years ago

@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?

kelunik commented 3 years ago

Try putting the try / catch around the Loop::run in that case.

eleimt commented 3 years ago

@kelunik Without changes

eleimt commented 3 years ago

Exist solutions?

kelunik commented 3 years ago

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.

eleimt commented 3 years ago

@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
kelunik commented 3 years ago

withHeader returns a new object, so you'll have to use $handshake = $handshake->withHeader(...).

eleimt commented 3 years ago

Okay, it's work. Thanks! @kelunik