Skip to content

New User Auth  #440

@ma-gu-16

Description

@ma-gu-16

Hi,

i changed all to the new api. works for javascript clients. they are authentificated.
but how does it work if i want to send messges to clients from server.

`
final class Client implements LoggerAwareInterface
{
use LoggerAwareTrait;

private const MSG_WELCOME = 0;
private const MSG_PREFIX = 1;
private const MSG_CALL = 2;
private const MSG_CALL_RESULT = 3;
private const MSG_CALL_ERROR = 4;
private const MSG_SUBSCRIBE = 5;
private const MSG_UNSUBSCRIBE = 6;
private const MSG_PUBLISH = 7;
private const MSG_EVENT = 8;

private WampRouter $router;
private PawlClient $client;

public function __construct(WampRouter $router, PawlClient $client)
{
    $this->router = $router;
    $this->client = $client;
}

/**
 * Calls a RPC handler on the websocket server.
 *
 * @param array $payload
 * @param string $routeName
 * @param array $routeParameters
 */
public function call(array $payload, string $routeName, array $routeParameters = []): void
{
    $this->client->connect()
        ->then(
            function (WebSocket $connection) use ($payload, $routeName, $routeParameters) {
                $this->logger->debug('Client connection resolved');
                $route = $this->router->generate($routeName, $routeParameters);
                $this->logger->debug('Calling RPC function on websocket server', ['route' => $route, 'payload' => $payload]);
                try {
                    $message = json_encode(array_merge([self::MSG_CALL, uniqid('', true), $route], $payload), JSON_THROW_ON_ERROR);

                    $connection->send($message);
                } catch (JsonException $exception) {
                    $this->logger->error('Could not encode message to call RPC function on websocket server.', ['exception' => $exception]);
                    throw $exception;
                } finally {
                    $connection->close();
                }
            },
            function (Throwable $throwable): void {
                $this->logger->error('Client connection rejected', ['exception' => $throwable]);
            }
        );
}

/**
 * Publishes to a Topic on the websocket server.
 *
 * @param array $payload
 * @param string $routeName
 * @param array $routeParameters
 */
public function publish(array $payload, string $routeName, array $routeParameters = []): void
{
    $this->client->connect()
        ->then(
            function (WebSocket $connection) use ($payload, $routeName, $routeParameters) {
                $this->logger->debug('Client connection resolved');
                $route = $this->router->generate($routeName, $routeParameters);
                $this->logger->debug('Publishing message to websocket server', ['route' => $route, 'payload' => $payload]);
                try {
                    $message = json_encode([self::MSG_PUBLISH, $route, $payload, [], []], JSON_THROW_ON_ERROR);

                    $connection->send($message);
                } catch (JsonException $exception) {
                    $this->logger->error('Could not encode message to publish to websocket server.', ['exception' => $exception]);

                    throw $exception;
                } finally {
                    $connection->close();
                }
            },
            function (Throwable $throwable): void {
                $this->logger->error('Client connection rejected', ['exception' => $throwable]);
            }
        );
}

}`

i used this service made everywhere i needed it an instance and -> publish()
but the websocketserver doesnt auth this call and creates anon.user how can i authentificate this call ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions