Example #1
0
 public function testRouterStopWithInternalClientLiveSession()
 {
     $loop = \React\EventLoop\Factory::create();
     $router = new \Thruway\Peer\Router($loop);
     // just so we have another transport
     $router->addTransportProvider(new \Thruway\Transport\RawSocketTransportProvider("127.0.0.1", 18080));
     $client = new \Thruway\Peer\Client("some_realm", $loop);
     $client->on('open', function () use($loop, $router) {
         $router->stop();
         $this->_result = "Stop was called";
     });
     $client->setAttemptRetry(false);
     // we are running on the same loop so if we allow retry, we will hang
     $router->addInternalClient($client);
     $loop->addTimer(0.1, function () use($client) {
         $client->start(false);
         // don't start loop yet
     });
     $router->start();
     // if the execution makes it here, stop worked
     $this->assertEquals("Stop was called", $this->_result);
 }
Example #2
0
 public function testAuthExtraSentBackOnAuthenticate()
 {
     $this->_result = null;
     $this->_error = null;
     $this->_resultPS = null;
     $this->_errorPS = null;
     $this->_challenged = false;
     $loop = \React\EventLoop\Factory::create();
     $router = new \Thruway\Peer\Router($loop);
     $router->registerModule(new \Thruway\Authentication\AuthenticationManager());
     $authClient = new TheAuthProvider(['my_realm'], $loop);
     $router->addInternalClient($authClient);
     $client = new \Thruway\Peer\Client('my_realm', $loop);
     $client->setAuthMethods(['extra_auth']);
     $client->setAuthId('authenticate_me');
     $client->setAttemptRetry(false);
     $callCount = 0;
     $stopOnSecondCall = function () use($loop, &$callCount) {
         $callCount++;
         if ($callCount == 2) {
             $loop->stop();
         }
     };
     $client->on('challenge', function (\Thruway\ClientSession $session, $msg) {
         $this->_challenged = true;
         $session->sendMessage(new \Thruway\Message\AuthenticateMessage("some_signature"));
     });
     $client->on('open', function (\Thruway\ClientSession $session) use($stopOnSecondCall) {
         // RPC stuff
         $session->register('get_the_authextra', function ($args, $argskw, $details) {
             return [$details];
         }, ['disclose_caller' => true])->then(function () use($session, $stopOnSecondCall) {
             $session->call('get_the_authextra')->then(function ($args) use($stopOnSecondCall) {
                 $this->_result = $args;
                 $stopOnSecondCall();
             }, function ($err) use($stopOnSecondCall) {
                 $this->_error = "call failed";
                 $stopOnSecondCall();
             });
         }, function () use($stopOnSecondCall) {
             $this->_error = "registration failed";
             $stopOnSecondCall();
         });
         // PubSub
         $session->subscribe('test_sub', function ($args, $argskw, $details) use($stopOnSecondCall) {
             $this->_resultPS = $details;
             $stopOnSecondCall();
         }, ["disclose_publisher" => true])->then(function () use($session, $stopOnSecondCall) {
             $session->publish('test_sub', [], null, ["exclude_me" => false, "acknowledge" => true])->then(function () use($stopOnSecondCall) {
             }, function () use($stopOnSecondCall) {
                 $this->_errorPS = "Error publishing";
                 $stopOnSecondCall();
             });
         }, function () use($stopOnSecondCall) {
             $this->_errorPS = "Error subscribing";
             $stopOnSecondCall();
         });
     });
     $router->addTransportProvider(new \Thruway\Transport\RatchetTransportProvider('127.0.0.1', 8087));
     $client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://127.0.0.1:8087/'));
     $loop->addTimer(5, function () use($loop) {
         $loop->stop();
     });
     $loop->addTimer(0.1, function () use($client) {
         $client->start(false);
     });
     $router->start();
     $this->assertNull($this->_error, $this->_error);
     $this->assertNull($this->_errorPS, $this->_errorPS);
     $this->assertNotNull($this->_result);
     $args = $this->_result;
     $this->assertArrayHasKey(0, $args);
     $this->assertTrue(is_object($args[0]));
     $this->assertObjectHasAttribute('_thruway_authextra', $args[0]);
     $this->assertNotNull($this->_resultPS);
     $this->assertTrue(is_object($this->_resultPS));
     $this->assertObjectHasAttribute('_thruway_authextra', $this->_resultPS);
     $this->assertEquals('authenticate', $this->_resultPS->_thruway_authextra->from);
     $this->assertTrue($this->_challenged, "We were supposed to be challenged");
 }
Example #3
0
<?php

require '../bootstrap.php';
$router = new \Thruway\Peer\Router();
$router->addTransportProvider(new \Thruway\Transport\RawSocketTransportProvider("127.0.0.1", 8181));
$router->start();
Example #4
0
| The composer auto-load class can be used to add lookup directories for
| custom application classes. This class can be assessed from the app
| controller using $app->loader.
| Alternatively you may use Budkit/Utitlity/Loader
|
*/
$loop = React\EventLoop\Factory::create();
$pusher = new \Thruway\Peer\Client("pubsub", $loop);
$pusher->on('open', function ($session) use($loop) {
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555');
    $pull->on('message', function ($message) use($session) {
        //messages should be sent as encode json?
        $data = json_decode($message, true);
        //must tell us the topic
        if (isset($data["topic"])) {
            $session->publish($data["topic"], [$data]);
        }
    });
});
$router = new Thruway\Peer\Router($loop);
$authMgr = new \Thruway\Authentication\AuthenticationManager();
//$router->setAuthenticationManager( $authMgr );
//$router->addTransportProvider( new \Thruway\Transport\InternalClientTransportProvider($authMgr));
$router->registerModule($authMgr);
//maybe register the auth as a module, extend module
$router->addInternalClient($app->createInstance(Helper\PubsubAuth::class, [$app, ["*"]]));
$router->addInternalClient($pusher);
$router->addTransportProvider(new Thruway\Transport\RatchetTransportProvider("0.0.0.0", 8080));
$router->start();