public function test() { $loop = \React\EventLoop\Factory::create(); $client = new \Thruway\Peer\Client('raw_realm', $loop); $client->addTransportProvider(new \Thruway\Transport\RawSocketClientTransportProvider()); $client->setAttemptRetry(false); $this->_result = null; $client->on('open', function (\Thruway\ClientSession $session) { $session->register('some_rpc', function () use($session) { $this->_result = "success"; $session->close(); })->then(function () use($session) { $session->call("some_rpc"); }); }); $client->start(); $this->assertNotNull($this->_result); }
public function testRouterStopWithRawSocketLiveSession() { $loop = \React\EventLoop\Factory::create(); $router = new \Thruway\Peer\Router($loop); $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 $client->addTransportProvider(new \Thruway\Transport\RawSocketClientTransportProvider("127.0.0.1", 18080)); $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); }
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"); }
<?php require '../vendor/autoload.php'; use Thruway\ClientSession; $client = new \Thruway\Peer\Client("realm1"); $client->on('open', function (ClientSession $session) { $session->call("users.setpresence", [], ["presence" => "away"])->then(function ($res) { print_r($res[0]); }); }); $client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider()); $client->start();