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.
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]);
}
);
}
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;
}`
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 ?