Advertisement
blaize

PHP Thruway Client Example

Apr 8th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. require __DIR__ . '/vendor/autoload.php';
  2.  
  3. use Thruway\ClientSession;
  4. use Thruway\Peer\Client;
  5. use Thruway\Transport\PawlTransportProvider;
  6.  
  7. $client = new Client("realm1");
  8. $client->addTransportProvider(new PawlTransportProvider("ws://127.0.0.1:9090/"));
  9.  
  10. $client->on('open', function (ClientSession $session) {
  11.  
  12. // 1) subscribe to a topic
  13. $onevent = function ($args) {
  14. echo "Event {$args[0]}\n";
  15. };
  16. $session->subscribe('com.myapp.hello', $onevent);
  17.  
  18. // 2) publish an event
  19. $session->publish('com.myapp.hello', ['Hello, world from PHP!!!'], [], ["acknowledge" => true])->then(
  20. function () {
  21. echo "Publish Acknowledged!\n";
  22. },
  23. function ($error) {
  24. // publish failed
  25. echo "Publish Error {$error}\n";
  26. }
  27. );
  28.  
  29. // 3) register a procedure for remoting
  30. $add2 = function ($args) {
  31. return $args[0] + $args[1];
  32. };
  33. $session->register('com.myapp.add2', $add2);
  34.  
  35. // 4) call a remote procedure
  36. $session->call('com.myapp.add2', [2, 3])->then(
  37. function ($res) {
  38. echo "Result: {$res}\n";
  39. },
  40. function ($error) {
  41. echo "Call Error: {$error}\n";
  42. }
  43. );
  44. });
  45.  
  46.  
  47. $client->start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement