protected function createTestRouter() { $this->assertNull($this->router); $this->router = new Router(\EventLoop\getLoop()); // create a client that records all publish events $this->recordedEvents = []; $this->eventClient = new Client("test_realm", \EventLoop\getLoop()); $this->eventClient->on('open', function (ClientSession $session) { $session->subscribe("wamppost.tests.", function ($args, $argsKw, $details, $pubId) { $eventMessage = new EventMessage(0, $pubId, $details, $args, $argsKw, $details->topic); array_push($this->recordedEvents, $eventMessage); }, (object) ["match" => "prefix"]); }); $this->router->addInternalClient($this->eventClient); return $this->router; }
/** * @param array $options * @param LoopInterface $loop * @throws \Exception */ function __construct(array $options, LoopInterface $loop = null, LoggerInterface $logger = null) { $this->options = $options; $this->client = new Client($options['realm'], $loop); /* * Add the transport provider * TODO: Allow for multiple transport providers */ $url = isset($options['url']) ? $options['url'] : null; $pawlTransport = new PawlTransportProvider($url); if ($logger) { $pawlTransport->getManager()->setLogger($logger); } $this->client->addTransportProvider($pawlTransport); $this->client->setReconnectOptions($options); /* * Authentication on challenge callback */ if (isset($options['onChallenge']) && is_callable($options['onChallenge']) && isset($options['authmethods']) && is_array($options['authmethods'])) { $this->client->setAuthMethods($options['authmethods']); $this->client->on('challenge', function (ClientSession $session, ChallengeMessage $msg) use($options) { $token = $options['onChallenge']($session, $msg->getAuthMethod()); $session->sendMessage(new AuthenticateMessage($token)); }); } if (isset($this->options['onClose']) && is_callable($this->options['onClose'])) { $this->on('close', $this->options['onClose']); } /* * Handle On Open event * */ $this->client->on('open', function (ClientSession $session, TransportInterface $transport) { $this->transport = $transport; $this->emit('open', [$session]); }); /* * Handle On Close event */ $this->client->on('close', function ($reason) { $this->emit('close', [$reason]); }); $this->client->on('error', function ($reason) { $this->emit('error', [$reason]); }); }
/** * Constructor * * @param array $options * @param \React\EventLoop\LoopInterface $loop * @throws \Exception */ public function __construct(array $options, LoopInterface $loop = null) { $this->options = $options; $this->client = new Client($options['realm'], $loop); $url = isset($options['url']) ? $options['url'] : null; $pawlTransport = new PawlTransportProvider($url); $this->client->addTransportProvider($pawlTransport); $this->client->setReconnectOptions($options); /* * Authentication on challenge callback */ if (isset($options['onChallenge']) && is_callable($options['onChallenge']) && isset($options['authmethods']) && is_array($options['authmethods'])) { $this->client->setAuthMethods($options['authmethods']); $this->client->on('challenge', function (ClientSession $session, ChallengeMessage $msg) use($options) { $token = call_user_func_array($options['onChallenge'], [$session, $msg->getAuthMethod(), $msg]); $session->sendMessage(new AuthenticateMessage($token)); }); } //Set Authid if (isset($options['authid'])) { $this->client->setAuthId($options['authid']); } if (isset($this->options['onClose']) && is_callable($this->options['onClose'])) { $this->on('close', $this->options['onClose']); } /* * Handle On Open event * */ $this->client->on('open', function (ClientSession $session, TransportInterface $transport) { $this->transport = $transport; $this->emit('open', [$session]); }); /* * Handle On Close event */ $this->client->on('close', function ($reason) { $this->emit('close', [$reason]); }); $this->client->on('error', function ($reason) { $this->emit('error', [$reason]); }); }
/** * Setup the onChallenge callback */ private function handleOnChallenge() { $options = $this->options; if (isset($options['onChallenge']) && is_callable($options['onChallenge']) && isset($options['authmethods']) && is_array($options['authmethods'])) { $this->client->setAuthMethods($options['authmethods']); $this->client->on('challenge', function (ClientSession $session, ChallengeMessage $msg) use($options) { $token = call_user_func_array($options['onChallenge'], [$session, $msg->getAuthMethod(), $msg]); $session->sendMessage(new AuthenticateMessage($token)); }); } }
/** * * @param \Thruway\ClientSession $session * @param \Thruway\Transport\TransportInterface $transport */ public function onSessionStart($session, $transport) { $session->register('add_command', [$this, 'addCommand']); $session->register('status', [$this, 'status']); $session->register('start_process', [$this, 'startProcess']); $session->register('stop_process', [$this, 'stopProcess']); $session->register('restart_process', [$this, 'restartProcess']); $session->register('add_instance', [$this, 'addInstance']); //Congestion Manager Client. This needs to be a separate client because it needs to listen on the main realm and not `process_manager`. $config = $this->container->getParameter('voryx_thruway'); $congestionManager = new Client($config['realm'], $session->getLoop()); $congestionManager->addTransportProvider(new PawlTransportProvider($config['trusted_url'])); $congestionManager->on('open', function (ClientSession $session) { $session->subscribe("thruway.metaevent.procedure.congestion", [$this, "onCongestion"]); }); $congestionManager->start(false); }
/** * @param Client $client */ public function setClient(Client $client) { $this->client = $client; $this->client->on('open', [$this, 'onOpen']); }
<?php require __DIR__ . '/vendor/autoload.php'; use Thruway\ClientSession; use Thruway\Peer\Client; use Thruway\Transport\PawlTransportProvider; $client = new Client("realm1"); $client->addTransportProvider(new PawlTransportProvider("ws://127.0.0.1:8080/ws")); $title = $_GET['title']; $message = $_GET['message']; $type = $_GET['type']; $notification['title'] = $title; $notification['message'] = $message; $notification['type'] = $type; $notification = json_encode($notification); echo "\n {$notification} \n"; //die(); $client->on('open', function (ClientSession $session) { global $notification; $session->publish('pcaudit.broadcastmessage', [$notification], [], ["acknowledge" => true])->then(function () { echo "Publish Acknowledged!\n"; die; }, function ($error) { // publish failed echo "Publish Error {$error}\n"; die; }); }); $client->start();
use Thruway\ClientSession; use Thruway\Peer\Client; use Thruway\Transport\PawlTransportProvider; $client = new Client("realm1"); $client->addTransportProvider(new PawlTransportProvider("ws://127.0.0.1:9090/")); $client->on('open', function (ClientSession $session) { // 1) subscribe to a topic $onevent = function ($args) { echo "Event {$args[0]}\n"; }; $session->subscribe('com.myapp.hello', $onevent); // 2) publish an event $session->publish('com.myapp.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(function () { echo "Publish Acknowledged!\n"; }, function ($error) { // publish failed echo "Publish Error {$error}\n"; }); // 3) register a procedure for remoting $add2 = function ($args) { return $args[0] + $args[1]; }; $session->register('com.myapp.add2', $add2); // 4) call a remote procedure $session->call('com.myapp.add2', [2, 3])->then(function ($res) { echo "Result: {$res}\n"; }, function ($error) { echo "Call Error: {$error}\n"; }); }); $client->start();