Guest User

Untitled

a guest
Mar 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. protected function execute(InputInterface $input, OutputInterface $output)
  2. {
  3. $loop = Factory::create();
  4. $pusher = new Notification();
  5.  
  6. $context = new Context($loop);
  7. $pull = $context->getSocket(ZMQ::SOCKET_PULL);
  8. $webSock = new Server('127.0.0.1:8080', $loop);
  9. $pull->bind('tcp://127.0.0.1:5555');
  10. $pull->on('message', array($pusher, 'onTest'));
  11. $webServer = new IoServer(
  12. new HttpServer(
  13. new WsServer(
  14. new Notification()
  15. )
  16. ),
  17. $webSock
  18. );
  19.  
  20. $loop->run();
  21. }
  22.  
  23. protected function execute(InputInterface $input, OutputInterface $output)
  24. {
  25. $context = new ZMQContext();
  26. $socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'onTest');
  27. $socket->connect('tcp://localhost:5555');
  28. $socket->send('Test message');
  29. }
  30.  
  31. class Notification implements MessageComponentInterface
  32. {
  33. protected $clients;
  34.  
  35. public function __construct()
  36. {
  37. $this->clients = new SplObjectStorage();
  38. }
  39.  
  40. public function onOpen(ConnectionInterface $conn)
  41. {
  42. $this->clients->attach($conn);
  43. }
  44.  
  45. public function onClose(ConnectionInterface $conn)
  46. {
  47. $this->clients->detach($conn);
  48. }
  49.  
  50. public function onError(ConnectionInterface $conn, Exception $e)
  51. {
  52. $conn->close();
  53. }
  54.  
  55. public function onMessage(ConnectionInterface $from, $msg)
  56. {
  57. //auth user etc
  58. }
  59.  
  60. public function onTest($entry)
  61. {
  62. //this array is always empty
  63. foreach ($this->clients as $client) {
  64. $client->send('send new message');
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment