react/zmq

(要求Workerman版本>=3.3.6)

安装:

  1. composer require react/zmq

示例:

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use Workerman\Worker;
  4. $worker = new Worker('text://0.0.0.0:6161');
  5. $worker->onWorkerStart = function() {
  6. global $pull;
  7. $loop = Worker::getEventLoop();
  8. $context = new React\ZMQ\Context($loop);
  9. $pull = $context->getSocket(ZMQ::SOCKET_PULL);
  10. $pull->bind('tcp://127.0.0.1:5555');
  11. $pull->on('error', function ($e) {
  12. var_dump($e->getMessage());
  13. });
  14. $pull->on('message', function ($msg) {
  15. echo "Received: $msg\n";
  16. });
  17. };
  18. Worker::runAll();

文档:

https://github.com/reactphp/zmq

注意:

1、所有的异步编码必须在onXXX回调中编写

2、异步客户端需要的$loop变量请使用Worker::getEventLoop();返回值