Inheritance: implements Evenement\EventEmitterInterface, use trait Evenement\EventEmitterTrait
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     //$simulations = Simulation::join('ItemSet as I', 'I.Id', '=', 'Simulation.Id')
     /*->where('Simulation.State', '!=', 0)
       ->where('Simulation.State', '!=', 3)*/
     //->select('I.*')
     //->first();
     //var_dump($simulations);
     //$s = Simulation::find('C4E3A3FB-9CE8-4FC6-81B9-5994B08EF1B2');
     //$this->info($s->State);
     $connection = new Connection(['realm' => "realm1", 'url' => "ws://127.0.0.1:8081/ws"]);
     $connection->on('open', function (ClientSession $session) use($connection) {
         $session->subscribe('com.gosmartsimulation.identify', function ($args) {
             $server_id = $args[0];
             $server_name = $args[1];
             $this->responses[$server_id] = true;
             $this->server_names[$server_id] = $server_name;
         });
         $this->tick($connection->getClient()->getLoop(), $session);
     });
     $connection->open();
     //Mail::send('emails.failed_sim', ['guid' => '1'], function ($message) {
     //  $message->to('*****@*****.**', 'Phil Weir')->subject('Simulation failed');
     //});
 }
 public function testAuthorizedActions()
 {
     $this->flushRules();
     $challenge = function ($session, $method) {
         return "letMeIn";
     };
     $this->_conn = new \Thruway\Connection(array("realm" => 'authful_realm', "url" => 'ws://127.0.0.1:8090', "max_retries" => 0, "authmethods" => ["simplysimple"], "onChallenge" => $challenge));
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->call("add_authorization_rule", [["role" => "sales", "action" => "publish", "uri" => "sales.numbers", "allow" => true]])->then(function ($r) {
             $this->_conn->close();
             $this->_testResult = "ok";
         }, function ($msg) {
             $this->_conn->close();
             $this->_testResult = "failed";
             $this->assertInstanceOf('\\Thruway\\Message\\ErrorMessage', $msg);
             $this->assertEquals("wamp.error.not_authorized", $msg->getErrorUri());
         });
     });
     $this->_conn->open();
     $this->assertEquals("failed", $this->_testResult);
     $this->_testResult = "";
     $this->_error = null;
     $this->_conn = new \Thruway\Connection(["realm" => 'authful_realm', "url" => 'ws://127.0.0.1:8090', "max_retries" => 0, "authmethods" => ["simplysimple"], "onChallenge" => function () {
         return "ozTheGreatAndPowerful";
     }]);
     // now set permissions to allow stuff
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->call("add_authorization_rule", [["role" => "sales", "action" => "call", "uri" => "add_authorization_rule", "allow" => true]])->then(function ($r) {
             $this->_conn->close();
             $this->_testResult = "ok";
             $this->assertEquals("ADDED", $r);
         }, function ($msg) {
             $this->_conn->close();
             $this->_error = "error adding authorization rule";
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Error: " . $this->_error);
     $this->assertEquals("ok", $this->_testResult);
     // now try to use the thing
     $this->_conn = new \Thruway\Connection(array("realm" => 'authful_realm', "url" => 'ws://127.0.0.1:8090', "max_retries" => 0, "authmethods" => ["simplysimple"], "onChallenge" => function () {
         return "letMeIn";
     }));
     $this->_testResult = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->call("add_authorization_rule", [["role" => "sales", "action" => "publish", "uri" => "sales.numbers", "allow" => true]])->then(function ($r) {
             $this->_conn->close();
             $this->_testResult = "success";
         }, function ($msg) {
             $this->_conn->close();
             $this->_testResult = "failed";
         });
     });
     $this->_conn->open();
     $this->assertEquals("success", $this->_testResult);
     $this->flushRules();
 }
 public function testReplaceOrphanedSession()
 {
     $this->_testResult = null;
     $this->_error = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $this->_conn->getClient()->getCallee()->register($session, 'com.example.orphan_testing', array($this, 'callOrphanTest'), ['replace_orphaned_session' => 'no'])->then(function ($res = null) use($session) {
             $this->_error = 'OrphaningClient not registered';
             $this->_conn->close();
         }, function ($msg = null) use($session) {
             if ($msg instanceof \Thruway\Message\ErrorMessage) {
                 if ($msg->getErrorURI() != 'wamp.error.procedure_already_exists') {
                     $this->_testResult = $msg->getErrorURI();
                     $this->_conn->close();
                 } else {
                     $this->_conn->getClient()->getCallee()->register($session, 'com.example.orphan_testing', array($this, 'callOrphanTest'), ['replace_orphaned_session' => 'yes'])->then(function () use($session) {
                         $session->call('com.example.orphan_testing', [])->then(function ($res) {
                             $this->_conn->close();
                             $this->_testResult = "resolve";
                         }, function ($error = null) {
                             if ($error instanceof \Thruway\Message\ErrorMessage) {
                                 $this->_testResult = $error->getErrorURI();
                             } else {
                                 $this->_testResult = "rejected";
                             }
                             $this->_conn->close();
                         });
                     });
                 }
             }
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Error was {$this->_error}");
     $this->assertEquals("resolve", $this->_testResult);
 }
Example #4
0
 public function testRegister()
 {
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->register('com.example.mul2', function ($args) {
             $this->_conn->close();
             $this->_testArgs = $args;
         });
     });
     $this->_conn->open();
     $this->assertGreaterThan(0, $this->_testArgs[0]);
 }
Example #5
0
 protected function onSuccess(\Thruway\Connection $connection, \Thruway\CallResult $result)
 {
     $connection->close();
     if ($result !== null) {
         $return = [];
         foreach ($result as $item) {
             $return[$item->name] = (array) $item;
         }
         return $return;
     }
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $this->prefix = $this->prefix . '';
     if ($this->option('server')) {
         $this->prefix .= '.' . $this->option('server');
     }
     $connection = new Connection(['realm' => "realm1", 'url' => "ws://127.0.0.1:8081/ws"]);
     $connection->on('open', function (ClientSession $session) use($connection) {
         $this->process_simulations($session, $connection);
     });
     $connection->open();
 }
Example #7
0
 /**
  * @depends testCall
  */
 public function testUnregister()
 {
     $this->_error = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $callback = function () {
             return "Hello";
         };
         $session->register('com.example.somethingToUnregister', $callback)->then(function () use($session) {
             $session->unregister('com.example.somethingToUnregister')->then(function () {
                 $this->_conn->close();
                 $this->_testResult = "unregistered";
             }, function () {
                 $this->_conn->close();
                 $this->_error = "Error during unregistration";
             });
         }, function () {
             $this->_conn->close();
             $this->_error = "Couldn't even register the call";
         });
         // TODO: test unregistering again
         // TODO: test unregistering a different session's registration
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Got this error when making an RPC call: {$this->_error}");
     $this->assertEquals('unregistered', $this->_testResult);
 }
Example #8
0
 public function testServerPing()
 {
     $this->_testResult = null;
     $this->_error = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->call('com.example.ping', [])->then(function ($res) {
             $this->_conn->close();
             $this->_testResult = $res;
         }, function ($error = null) {
             if ($error instanceof \Thruway\Message\ErrorMessage) {
                 $this->_error = $error->getErrorURI();
             }
             $this->_conn->close();
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Error is null");
     $this->assertTrue(is_numeric($this->_testResult[0]), "Server ping returned a success");
 }
 public function testCheckHelloDetails()
 {
     $this->_error = null;
     $this->_testResult = null;
     $roleInfo = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) use(&$roleInfo) {
         $roleInfo = $this->_conn->getClient()->getRoleInfoObject();
         $session->call('com.example.get_hello_details')->then(function ($r) use($session) {
             $this->_testResult = $r;
             $session->close();
         }, function ($r) use($session) {
             $this->_error = $r;
             $session->close();
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Got this error when making an RPC call: {$this->_error}");
     $this->assertEquals($roleInfo, $this->_testResult[0]);
 }
Example #10
0
 public function testCall()
 {
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $add2 = function ($args, $kwargs, $details) {
             $this->_testCallerId = $details["caller"];
             $this->_testAuthId = $details["authid"];
             $this->_testAuthMethod = $details["authmethod"];
             //                    $this->_testAuthRole = $details["authrole"];
             return $args[0] + $args[1];
         };
         $session->register('com.example.disclosecallertest', $add2, ['disclose_caller' => true])->then(function () use($session) {
             $session->call('com.example.disclosecallertest', [1, 2])->then(function ($res) {
                 $this->_conn->close();
                 $this->_testResult = $res;
             }, function ($error) {
                 $this->_conn->close();
                 $this->_error = $error;
             });
         });
     });
     $this->_conn->getClient()->setAuthId("*****@*****.**");
     $this->_conn->open();
     $this->assertNull($this->_error, "Got this error when making an RPC call: {$this->_error}");
     $this->assertEquals(3, $this->_testResult[0]);
     $this->assertNotEmpty($this->_testCallerId);
     $this->assertEquals("*****@*****.**", $this->_testAuthId);
     $this->assertEquals("simplysimple", $this->_testAuthMethod);
 }
 public function testSubscribe()
 {
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         /**
          * Subscribe to event
          */
         $session->subscribe('com.example.publish', function ($args, $kwargs = null, $details, $publicationId = null) {
             $this->_testArgs = $args;
             $this->_testPublisherId = $details->publisher;
             $this->_testTopic = $details->topic;
             $this->_testAuthId = $details->authid;
             $this->_testAuthMethod = $details->authmethod;
             $this->_testAuthRole = $details->authroles;
         }, ['disclose_publisher' => true])->then(function () use($session) {
             /**
              * Tell the server to publish
              */
             $session->call('com.example.publish', ['test publish'])->then(function ($res) {
                 $this->_testResult = $res;
             }, function ($error) {
                 $this->_conn->close();
                 $this->_error = $error;
             })->then(function () use($session) {
                 $session->close();
             });
         }, function () use($session) {
             $session->close();
             throw new Exception("subscribe failed.");
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Got this error when receiving Event: {$this->_error}");
     $this->assertEquals('ok', $this->_testResult[0]);
     $this->assertNotEmpty($this->_testPublisherId);
     $this->assertEquals("anonymous", $this->_testAuthId);
     $this->assertEquals("internalClient", $this->_testAuthMethod);
     $this->assertEquals('com.example.publish', $this->_testTopic);
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $this->prefix = 'com.gosmartsimulation';
     if ($this->option('server')) {
         $this->prefix .= '.' . $this->option('server');
     }
     $connection = new Connection(['realm' => "realm1", 'url' => "ws://127.0.0.1:8081/ws"]);
     $connection->on('open', function (ClientSession $session) use($connection) {
         $onComplete = function ($args) use($session) {
             $this->completed_simulation($args, $session);
         };
         $session->subscribe('com.gosmartsimulation.complete', $onComplete);
         $onFail = function ($args) use($session) {
             $this->failed_simulation($args, $session);
         };
         $session->subscribe('com.gosmartsimulation.fail', $onFail);
         $onStatus = function ($args) use($session) {
             $this->status_simulation($args, $session);
         };
         $session->subscribe('com.gosmartsimulation.status', $onStatus);
         $this->info('Subscribed');
     });
     $connection->open();
 }
Example #13
0
 public function testWhiteList()
 {
     $conns = [];
     $sessionIds = [];
     $results = [];
     $loop = $this->_loop;
     $subscribePromises = [];
     for ($i = 0; $i < 5; $i++) {
         $results[$i] = "";
         $conns[$i] = $conn = new \Thruway\Connection($this->_connOptions, $loop);
         $conn->on('open', function (\Thruway\ClientSession $session) use($i, &$sessionIds, &$subscribePromises, &$results) {
             $sessionIds[$i] = $session->getSessionId();
             $subscribePromises[] = $session->subscribe('test.whitelist', function ($args) use($i, $session, &$results) {
                 $results[$i] .= "-" . $args[0] . "." . $i . "-";
                 if ($args[0] == "X") {
                     $session->close();
                 }
             });
         });
         $conn->open(false);
     }
     $this->_conn->on('open', function (\Thruway\ClientSession $session) use($subscribePromises, &$sessionIds) {
         \React\Promise\all($subscribePromises)->then(function () use($session, &$sessionIds) {
             $session->publish('test.whitelist', ["A"], null, ['acknowledge' => true])->then(function () use($session, &$sessionIds) {
                 $session->publish('test.whitelist', ["B"], null, ['acknowledge' => true, 'eligible' => $sessionIds])->then(function () use($session, &$sessionIds) {
                     $session->publish('test.whitelist', ["C"], null, ['acknowledge' => true, 'exclude' => [$sessionIds[1]], 'eligible' => $sessionIds])->then(function () use($session, &$sessionIds) {
                         $session->publish('test.whitelist', ["D"], null, ['acknowledge' => true, 'exclude' => [$sessionIds[1]], 'eligible' => [$sessionIds[2]]])->then(function () use($session, &$sessionIds) {
                             $session->publish('test.whitelist', ["E"], null, ['acknowledge' => true, 'exclude' => [$sessionIds[1]], 'eligible' => []])->then(function () use($session, &$sessionIds) {
                                 $session->publish('test.whitelist', ["F"], null, ['acknowledge' => true, 'exclude' => [], 'eligible' => [$sessionIds[0]]])->then(function () use($session, &$sessionIds) {
                                     // shutdown the sessions
                                     $session->publish('test.whitelist', ["X"], null, ['acknowledge' => true])->then(function () use($session) {
                                         $session->close();
                                     });
                                 });
                             });
                         });
                     });
                 });
             });
         });
     });
     $this->_conn->open();
     $this->assertEquals("-A.0--B.0--C.0--F.0--X.0-", $results[0]);
     $this->assertEquals("-A.1--B.1--X.1-", $results[1]);
     $this->assertEquals("-A.2--B.2--C.2--D.2--X.2-", $results[2]);
     $this->assertEquals("-A.3--B.3--C.3--X.3-", $results[3]);
     $this->assertEquals("-A.4--B.4--C.4--X.4-", $results[4]);
 }
Example #14
0
 function __construct()
 {
     $this->options = \Drupal::config('thruway.settings')->get('options');
     $loop = \Drupal::service('thruway.loop');
     parent::__construct($this->options, $loop, new ConsoleLogger());
     $this->pluginManager = \Drupal::service('thruway.plugin.manager');
     $this->serializer = \Drupal::service('serializer');
     $this->annotationReader = new AnnotationReader();
     $this->resources = \Drupal::config('thruway.settings')->get('resources');
     //Register the Thruway annotation
     AnnotationRegistry::registerFile(\Drupal::service('module_handler')->getModuleList()['thruway']->getPath() . "/src/Annotation/Thruway.php");
     $this->on('open', [$this, 'onSessionStart']);
     $this->on('close', function ($reason) {
         $this->getClient()->getLogger()->alert("Connection closing because: {$reason}");
     });
     //        $this->getClient()->setLogger(\Drupal::logger("thruway"));
     $this->getClient()->setLogger(new ConsoleLogger());
 }
Example #15
0
use Thruway\ClientWampCraAuthenticator;
use Thruway\Connection;
use Thruway\Logging\Logger;
use Thruway\Message\ChallengeMessage;
require __DIR__ . '/vendor/autoload.php';
Logger::set(new NullLogger());
$user = "******";
$password = "******";
$onChallenge = function (ClientSession $session, $method, ChallengeMessage $msg) use($user, $password) {
    if ("wampcra" !== $method) {
        return false;
    }
    $cra = new ClientWampCraAuthenticator($user, $password);
    return $cra->getAuthenticateFromChallenge($msg)->getSignature();
};
$connection = new Connection(["realm" => 'realm1', "url" => 'ws://127.0.0.1:8080/ws', "authmethods" => ["wampcra"], "onChallenge" => $onChallenge, "authid" => $user]);
$connection->on('open', function (ClientSession $session) use($connection) {
    echo "connected session with ID {$session->getSessionId()}";
    // call a procedure we are allowed to call (so this should succeed)
    //
    $session->call('com.example.add2', [2, 3])->then(function ($res) {
        echo "call result: {$res}\n";
    }, function ($error) {
        if ($error !== 'wamp.error.no_such_procedure') {
            echo "call error: {$error}\n";
        }
    });
    // (try to) register a procedure where we are not allowed to (so this should fail)
    //
    $session->register('com.example.mul2', function ($args) {
        return $args[0] * $args[1];
 /**
  * Make WAMP call
  *
  * @param $uri
  * @param array $args
  * @return null
  */
 private function call($uri, $args = [])
 {
     $result = null;
     $realm = 'process_manager';
     $connection = new Connection(['realm' => $realm, 'url' => $this->config['trusted_url'], "max_retries" => 0]);
     $connection->on('open', function (ClientSession $session) use($uri, $args, $connection, &$result) {
         $session->call($uri, $args)->then(function ($res) use($connection, &$result) {
             $result = $res[0];
             $connection->close();
         }, function ($error) use($connection, &$result) {
             $result = $error;
             $connection->close();
         });
     });
     $connection->open();
     return $result;
 }
    echo "authenticate called: {$realm}, {$authid}, {$details}\n";
    if (isset($userDb[$authid])) {
        return $userDb[$authid];
    }
    echo "no such user: {$realm}, {$authid}\n";
};
$user = $argv[3];
$password = $argv[4];
$onChallenge = function (ClientSession $session, $method, ChallengeMessage $msg) use($user, $password) {
    if ("wampcra" !== $method) {
        return false;
    }
    $cra = new ClientWampCraAuthenticator($user, $password);
    return $cra->getAuthenticateFromChallenge($msg)->getSignature();
};
$connection = new Connection(["realm" => $argv[2], "url" => $argv[1], "authmethods" => ["wampcra"], "onChallenge" => $onChallenge, "authid" => $user]);
$connection->on('open', function (ClientSession $session) use($connection, $authenticate) {
    echo "custom authenticator connected\n";
    $session->register('com.example.authenticate', $authenticate)->then(function () {
        echo "Ok, custom WAMP-CRA authenticator procedure registered\n";
    }, function ($error) {
        echo "Uups, could not register custom WAMP-CRA authenticator {$error}\n";
    });
});
$connection->on('close', function ($reason) {
    echo "The authenticator client connection has closed with reason: {$reason}\n";
});
$connection->on('error', function ($reason) {
    echo "The authenticator client connection has closed with error: {$reason}\n";
});
$connection->open();
Example #18
0
##  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
##  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
##  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
##  POSSIBILITY OF SUCH DAMAGE.
##
###############################################################################
use Psr\Log\NullLogger;
use Thruway\ClientSession;
use Thruway\Connection;
use Thruway\Logging\Logger;
require __DIR__ . '/vendor/autoload.php';
//Uncomment to disable logging
//Logger::set(new NullLogger());
$timer = null;
$loop = React\EventLoop\Factory::create();
$connection = new Connection(["realm" => 'realm1', "url" => 'ws://127.0.0.1:8080/ws'], $loop);
$connection->on('open', function (ClientSession $session) use($connection, $loop, &$timer) {
    // SUBSCRIBE to a topic and receive events
    $onHello = function ($args) {
        echo "event for 'onhello' received: {$args[0]}\n";
    };
    $session->subscribe('com.example.onhello', $onHello);
    echo "subscribed to topic 'onhello'";
    // REGISTER a procedure for remote calling
    $add2 = function ($args) {
        echo "add2() called with {$args[0]} and {$args[1]}\n";
        return $args[0] + $args[1];
    };
    $session->register('com.example.add2', $add2);
    echo "procedure add2() registered\n";
    $counter = 0;
 public function testMultiRegistration()
 {
     $this->_error = null;
     $this->_result = null;
     $this->_conn->on('open', function (\Thruway\ClientSession $session) {
         $session->register('partitioned_rpc', [$this, "thePartitionedRPC1"], ["thruway_multiregister" => true])->then(function ($ret) use($session) {
             // this should fail because we are registering an identical rpc from the same connection
             $session->register('partitioned_rpc', [$this, "thePartitionedRPC1"])->then(function () use($session) {
                 $this->_error = "Second registration from same connection should have failed";
                 $session->close();
             }, function () use($session) {
                 $this->_conn2->on('open', function (\Thruway\ClientSession $s2) use($session) {
                     // this should fail without thruway_multiregister
                     $s2->register('partitioned_rpc', [$this, "thePartitionedRPC2"])->then(function () use($s2, $session) {
                         $this->_error = "Second registration without multiregister option should have failed";
                         $s2->close();
                         $session->close();
                     }, function () use($s2, $session) {
                         // this should be the 2nd RPC
                         $s2->register('partitioned_rpc', [$this, "thePartitionedRPC2"], ["thruway_multiregister" => true])->then(function () use($s2, $session) {
                             // good - we have 2 registrations - lets make some calls
                             $this->_result = "";
                             $session->subscribe('thruway.metaevent.procedure.congestion', function ($args) {
                                 if (isset($args[0]) && isset($args[0]['name'])) {
                                     $procName = $args[0]['name'];
                                 } else {
                                     $procName = "";
                                 }
                                 $this->_result = $this->_result . "[congestion(" . $procName . ")]";
                             })->then(function () use($s2, $session) {
                                 $promises = [];
                                 $promises[] = $session->call('partitioned_rpc', [0])->then(function ($res) use($s2, $session) {
                                     $this->_result = $this->_result . $res;
                                 }, function ($err) use($s2, $session) {
                                     $this->_error = "Error calling RPC attempt 1";
                                     $s2->close();
                                     $session->close();
                                 });
                                 $promises[] = $session->call('partitioned_rpc', [1])->then(function ($res) use($s2, $session) {
                                     $this->_result = $this->_result . $res;
                                 }, function ($err) use($s2, $session) {
                                     $this->_error = "Error calling RPC(2)";
                                     $s2->close();
                                     $session->close();
                                 });
                                 $promises[] = $session->call('partitioned_rpc', [2])->then(function ($res) use($s2, $session) {
                                     $this->_result = $this->_result . $res;
                                 }, function ($err) use($s2, $session) {
                                     $this->_error = "Error calling RPC";
                                     $s2->close();
                                     $session->close();
                                 });
                                 $pAll = \React\Promise\all($promises);
                                 $pAll->then(function () use($s2, $session) {
                                     $s2->close();
                                     $session->close();
                                 }, function ($err) {
                                     $this->_error = "Could not subscribe to metaevent";
                                 });
                             });
                         }, function () use($s2, $session) {
                             $this->_error = "Second registration failed with multiregister";
                             $s2->close();
                             $session->close();
                         });
                     });
                 });
                 $this->_conn2->open();
             });
         }, function ($err) use($session) {
             $this->_error = "First registration failed";
             var_dump($err);
             $session->close();
         });
     });
     $this->_conn->open();
     $this->assertNull($this->_error, "Error occurred: " . $this->_error);
     $this->assertEquals("211", $this->_result);
 }
Example #20
0
use Thruway\ClientWampCraAuthenticator;
use Thruway\Connection;
use Thruway\Logging\Logger;
use Thruway\Message\ChallengeMessage;
require __DIR__ . '/vendor/autoload.php';
Logger::set(new NullLogger());
$user = $argv[3];
$password = $argv[4];
$onChallenge = function (ClientSession $session, $method, ChallengeMessage $msg) use($user, $password) {
    if ("wampcra" !== $method) {
        return false;
    }
    $cra = new ClientWampCraAuthenticator($user, $password);
    return $cra->getAuthenticateFromChallenge($msg)->getSignature();
};
$connection = new Connection(["realm" => $argv[2], "url" => $argv[1], "authmethods" => ["wampcra"], "onChallenge" => $onChallenge, "authid" => $user]);
$connection->on('open', function (ClientSession $session) use($connection) {
    echo "backend connected session with ID {$session->getSessionId()}\n";
    // Subscribe to topics
    //
    $topics = ['com.example.topic1', 'com.example.topic2', 'com.foobar.topic1', 'com.foobar.topic2'];
    foreach ($topics as $topic) {
        $onHello = function ($args) use($topic) {
            $msg = array_shift($args);
            echo "event received on topic  {$topic}: {$msg}\n";
        };
        $session->subscribe($topic, $onHello)->then(function ($pub) use($topic) {
            echo "ok, subscribed to topic {$topic}\n";
        }, function ($error) use($topic) {
            echo "could not subscribe to topic {$topic} failed: {$error}\n";
        });
Example #21
0
<?php

require 'bootstrap.php';
use Thruway\ClientSession;
use Thruway\Connection;
$onClose = function ($msg) {
    echo $msg;
};
$connection = new Connection(array("realm" => 'realm1', "onClose" => $onClose, "url" => 'ws://127.0.0.1:9090'));
$connection->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', array('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', array(2, 3))->then(function ($res) {
        echo "Result: {$res}\n";
    }, function ($error) {