高速公路WAMP2上的内部客户

时间:2014-10-02 03:40:50

标签: php serversocket phpwebsocket thruway

我正在研究内部客户端+身份验证模型的示例,如下所示。 现在,我需要检索已连接会话的列表,并从内部客户端拦截会话的关闭事件。 我想问一下是否有任何方法来存档该任务?我正在考虑在redis中保存该列表,但这意味着我必须重新编写Thruway\Peer\Router类,因为所需的变量现在是私有的,我们无权访问它们。

文件server.php

    <?php
    /**
     * server.php
     */

    require "../bootstrap.php";
    require 'InternalClient.php';
    require 'SimpleAuthProviderClient.php';

    use Thruway\Peer\Router;
    use Thruway\Transport\RatchetTransportProvider;
    use React\EventLoop\Factory;
    use Thruway\Manager\ManagerClient;
    use Thruway\Transport\InternalClientTransportProvider;

    $manager = new ManagerClient();
    $loop = Factory::create();

    $router = new Router($loop, $manager);
    $router->addTransportProvider(new InternalClientTransportProvider($manager));

    $internalTransportProvider = new InternalClientTransportProvider(new \InternalClient());
    $router->addTransportProvider($internalTransportProvider);

    $authMgr = new \Thruway\Authentication\AuthenticationManager();

    $router->setAuthenticationManager($authMgr);
    $router->addTransportProvider(new InternalClientTransportProvider($authMgr));

    //Provide authentication for the realm: 'somerealm'
    $authProvClient = new SimpleAuthProviderClient(["somerealm"]);
    $router->addTransportProvider(new InternalClientTransportProvider($authProvClient));

    $transportProvider = new RatchetTransportProvider("127.0.0.1", 9090);
    $router->addTransportProvider($transportProvider);

    $router->start();

文件SimpleAuthProviderClient.php

    <?php
    /**
     * SimpleAuthProviderClient.php
     */
    require "../bootstrap.php";

    /**
     * Class SimpleAuthProviderClient
     */
    class SimpleAuthProviderClient extends \Thruway\Authentication\AbstractAuthProviderClient
    {

        /**
         * @return string
         */
        public function getMethodName()
        {
            return 'simplysimple';
        }

        /**
         * @param mixed $signature
         * @param null $extra
         * @return array
         */
        public function processAuthenticate($signature, $extra = null)
        {
            if ($signature == "letMeIn") {
                return ["SUCCESS"];
            } else {
                return ["FAILURE"];
            }

        }

    }

文件InternalClient.php

    <?php
    /**
     * InternalClient.php
     */
    require "../bootstrap.php";

    /**
     * Class InternalClient
     */
    class InternalClient extends Thruway\Peer\Client
    {

        function __construct()
        {
            parent::__construct("realm1");
        }

        /**
         * @param \Thruway\AbstractSession $session
         * @param \Thruway\Transport\TransportInterface $transport
         */
        public function onSessionStart($session, $transport)
        {
            echo "--------------- Hello from InternalClient ------------";
            $this->getCallee()->register($this->session, 'com.example.getphpversion', [$this, 'getPhpVersion']);
        }


        function start()
        {
        }

        /**
         * @return array
         */
        function getPhpVersion()
        {
            return [phpversion()];
        }
    }

1 个答案:

答案 0 :(得分:2)

出于参考目的,Github回答了这个问题。

如果您订阅了活动wamp.metaevent.session.on_joinwamp.metaevent.session.on_leave,您会收到通知。该事件返回1个与此类似的参数:

{ "realm":"realm1", "authprovider":null, "authid":"username", "authrole":"none", "authmethod":"simplysimple", "session":6016528494456948 }