首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Thruway管理订阅

Thruway管理订阅
EN

Stack Overflow用户
提问于 2018-05-07 09:24:30
回答 1查看 1.2K关注 0票数 1

我试图通过Thruway设置一个websocket服务器,它可以管理多个组。类似于聊天应用程序,每个客户端可以同时订阅一个或多个应用程序,并将消息广播到整个聊天室。我用一个古老版本的棘轮成功地做到了这一点,但由于它的运行并不顺利,所以我想改用Thruway。遗憾的是,我找不到任何东西来管理团队。到目前为止,我有以下内容,因为websocket管理器和客户端使用的是当前版本的Autobahn\js (18.x)。

如果有可能使用以下内容管理订阅组,是否有人有任何头绪?

代码语言:javascript
复制
<?php

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

use Thruway\Peer\Router;
use Thruway\Transport\RatchetTransportProvider;

$router = new Router();
$router->addTransportProvider(new RatchetTransportProvider("0.0.0.0", 9090));

$router->start();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-07 17:44:19

对ThruWay来说,情况与过去的棘轮略有不同。首先,Thruway不是WAMP服务器。它只是一个路由器。因此,它没有像旧的Rathcet那样的服务器实例,它允许您包装所有服务器端功能。但是它只会获得消息包,并根据订阅情况将其route到同一领域中的其他会话。如果您曾经使用过socket.io,则领域概念类似于不同的连接,因此您可以将会话或连接限制在单个命名空间中,或者将不同套接字实例(如管理、访问者等)的功能拆分。

在使用autobahn (最新版本)的客户端,一旦您订阅了某个主题,然后在该主题中发布,thruway将自动检测主题订阅者并在同一领域向他们发出消息。但是在旧的棘轮中,您需要手动处理这个问题,方法是保留一个可用的通道数组,并在用户订阅时将用户添加到每个通道,并通过迭代这些用户的主题向这些用户广播消息。这真的很痛苦。

如果您想在服务器端使用RPC调用,并且不想在客户端包含一些内容,那么仍然可以在服务器端使用一个名为internalClient的类。从概念上讲,内部客户端是另一个会话,它连接到您的通道客户端,并在内部处理一些功能,而不公开其他客户端。它接收消息包并在其中执行操作,然后将结果返回给请求的客户端连接。我花了一段时间才明白它是如何工作的,但一旦我明白了背后的想法就更有意义了。

所以一点点代码可以解释得更好,

在您的路由器实例中,您将需要添加一个模块(注意,在voxys/thruway包中,示例对内部客户端不太容易混淆)

server.php

代码语言:javascript
复制
require __DIR__ . "/../bootstrap.php";
require __DIR__ . '/InternalClient.php';

$port = 8080;
$output->writeln([
    sprintf('Starting Sockets Service on Port [%s]', $port),
]);
$router = new Router();

$router->registerModule(new RatchetTransportProvider("127.0.0.1", $port));   // use 0.0.0.0 if you want to expose outside world

// common realm ( realm1 )
$router->registerModule(
    new InternalClient()    // instantiate the Socket class now
);

// administration realm (administration)
// $router->registerModule(new \AdminClient());

$router->start();

这将初始化Thruway路由器,并将内部客户端实例附加到它。现在,在InternalClient.php文件中,您将能够访问实际路由以及当前连接的客户端。通过他们提供的示例,路由器不是实例的一部分,所以您只能使用新连接的会话id属性。

InternalClient.php

代码语言:javascript
复制
<?php

use Thruway\Module\RouterModuleInterface;
use Thruway\Peer\Client;
use Thruway\Peer\Router;
use Thruway\Peer\RouterInterface;
use Thruway\Logging\Logger;
use React\EventLoop\LoopInterface;

class InternalClient extends Client implements RouterModuleInterface
{
    protected $_router;

    /**
     * Contructor
     */
    public function __construct()
    {
        parent::__construct("realm1");
    }

    /**
     * @param RouterInterface $router
     * @param LoopInterface $loop
     */
    public function initModule(RouterInterface $router, LoopInterface $loop)
    {
        $this->_router = $router;

        $this->setLoop($loop);

        $this->_router->addInternalClient($this);
    }

    /**
     * @param \Thruway\ClientSession $session
     * @param \Thruway\Transport\TransportInterface $transport
     */
    public function onSessionStart($session, $transport)
    {
        // TODO: now that the session has started, setup the stuff

        echo "--------------- Hello from InternalClient ------------\n";
        $session->register('com.example.getphpversion', [$this, 'getPhpVersion']);

        $session->subscribe('wamp.metaevent.session.on_join',  [$this, 'onSessionJoin']);
        $session->subscribe('wamp.metaevent.session.on_leave', [$this, 'onSessionLeave']);
    }

    /**
     * Handle on new session joined.
     * This is where session is initially created and client is connected to socket server
     *
     * @param array $args
     * @param array $kwArgs
     * @param array $options
     * @return void
     */
    public function onSessionJoin($args, $kwArgs, $options) {
        $sessionId = $args && $args[0];
        $connectedClientSession = $this->_router->getSessionBySessionId($sessionId);
        Logger::debug($this, 'Client '. $sessionId. ' connected');
    }

    /**
     * Handle on session left.
     *
     * @param array $args
     * @param array $kwArgs
     * @param array $options
     * @return void
     */
    public function onSessionLeave($args, $kwArgs, $options) {

        $sessionId = $args && $args[0];

        Logger::debug($this, 'Client '. $sessionId. ' left');

        // Below won't work because once this event is triggered, client session is already ended
        // and cleared from router. If you need to access closed session, you may need to implement
        // a cache service such as Redis to access data manually.
        //$connectedClientSession = $this->_router->getSessionBySessionId($sessionId); 
    }

    /**
     * RPC Call messages
     * These methods will run internally when it is called from another client. 
     */
    private function getPhpVersion() {

        // You can emit or broadcast another message in this case
        $this->emitMessage('com.example.commonTopic', 'phpVersion', array('msg'=> phpVersion()));

        $this->broadcastMessage('com.example.anotherTopic', 'phpVersionRequested', array('msg'=> phpVersion()));

        // and return result of your rpc call back to requester
        return [phpversion()];
    }

    /**
     * @return Router
     */
    public function getRouter()
    {
        return $this->_router;
    }


    /**
     * @param $topic
     * @param $eventName
     * @param $msg
     * @param null $exclude
     */
    protected function broadcastMessage($topic, $eventName, $msg)
    {
        $this->emitMessage($topic, $eventName, $msg, false);
    }

    /**
     * @param $topic
     * @param $eventName
     * @param $msg
     * @param null $exclude
     */
    protected function emitMessage($topic, $eventName, $msg, $exclude = true)
    {
        $this->session->publish($topic, array($eventName), array('data' => $msg), array('exclude_me' => $exclude));
    }

}

在上面的示例代码中需要注意的事情很少--为了在主题中接收消息,在客户端您需要订阅该主题。-内部客户端可以发布/发出/广播任何主题,而无需在同一领域订阅。-广播/发射功能不是原来的高速公路的一部分,这是我想出的东西,使我的出版物在我的一端稍微容易一些。发送将发送消息包给已订阅主题的每个人,但发件人除外。另一方面,广播不排除发送者。

我希望这些资料能对理解这个概念有所帮助。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50210887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档