Open karelVanGeerdeghom opened 5 years ago
I've gotten a little further:
I've added an authorization manager thus: voryx.yaml:
voryx_thruway:
...
router:
...
authorization: app.security.websocket.authorization.manager
services:
app.security.websocket.authorization.manager:
class: App\Security\Websocket\Authorization\AuthorizationManager
arguments:
- "@=parameter('voryx_thruway')['realm']"
- '@voryx.thruway.loop'
Class:
<?php
namespace App\Security\Websocket\Authorization;
use Thruway\Event\MessageEvent;
use Thruway\Module\RealmModuleInterface;
use Thruway\Module\RouterModuleClient;
class AuthorizationManager extends RouterModuleClient implements RealmModuleInterface
{
public function getSubscribedRealmEvents()
{
return [
'PublishMessageEvent' => ['authorize', 100],
'SubscribeMessageEvent' => ['authorize', 100],
'RegisterMessageEvent' => ['authorize', 100],
'CallMessageEvent' => ['authorize', 100],
];
}
public function onSessionStart($session, $transport)
{
parent::onSessionStart($session, $transport);
$session->subscribe('wamp.metaevent.session.on_join', [$this, 'onSessionJoin']);
$session->subscribe('wamp.metaevent.session.on_leave', [$this, 'onSessionLeave']);
}
public function onSessionJoin($args, $kwArgs, $options)
{
var_dump('onSessionJoin');
}
public function onSessionLeave($args, $kwArgs, $options)
{
var_dump('onSessionLeave');
}
public function authorize(MessageEvent $msg)
{
var_dump($msg);
return true;
}
}
I can see the onSessionJoin and onSessionLeave events in my CLI, but when I try to publish, subscribe or call, nothing happens.
So adding trusted_url and trusted_port allows me to publish from server to a specific user by authid (ie. user id), thusly: voryx.yaml:
voryx_thruway:
...
trusted_url: 'ws://<ip>:<port>'
router:
...
trusted_port: '<port>'
services:
app.service.websocket.publisher:
class: App\Service\Websocket\Publisher\PublisherService
arguments:
- '@thruway.client'
Class:
<?php
namespace App\Service\Websocket\Publisher;
use Voryx\ThruwayBundle\Client\ClientManager;
class PublisherService
{
private $clientManager;
public function __construct
(
ClientManager $clientManager
)
{
$this->clientManager = $clientManager;
}
public function publish(string $topic, string $message, int $authid)
{
$this->clientManager->publish($topic, [$message], [], ['_thruway_eligible_authids' => [$authid]]);
}
}
K, yet more implementation: I can now register when client subscribes (with https://github.com/voryx/ThruwaySubscriptionMeta)
use Thruway\Module\SubscriptionMetaModule;
class AuthorizationManager extends RouterModuleClient implements RealmModuleInterface
{
...
public function onSessionStart($session, $transport)
{
...
$this->router->getRealmManager()->getRealm('<realm>')->addModule(new SubscriptionMetaModule());
$session->subscribe('wamp.subscription.on_subscribe', [$this, 'onSubscribe']);
$session->subscribe('wamp.subscription.on_unsubscribe', [$this, 'onUnsubscribe']);
}
...
public function onSubscribe($args, $kwArgs, $options)
{
var_dump('onSubscribe');
}
public function onUnsubscribe($args, $kwArgs, $options)
{
var_dump('onUnsubscribe');
}
}
Still unable to publish and call from client
Hey,
I've created a worker service as follows:
voryx.yaml:
Class:
Client:
When I set authentication to false under voryx_thruway.router, I get the in my console as result '11'. All ok sofar. Also on subscribing to 'websocket.subscription', when I subsequently do the publish, I get the response I'm expecting.
Then I implemnet authentication following this post http://voryx.net/integrating-symfony-authentication-for-thruwaybundle/:
voryx.yaml:
class
Now authentication works when I create a connection from client side as such:
As long as my token is still valid, connection succeeds. However, the 'websocket.call' and 'websocket.publish' no longer work. I've put some logging in my worker __construct method, and it logs nothing. When I set voryx_thruway.router.authentication back to false, I get logging and everything works again.
I'm using latest versions of Symfony 4, this bundle and Autobahn.js.
What am I doing wrong?