当前位置: 首页 > 文档资料 > WorkerMan 3.x 手册 >

异步消息队列组件 - react/zmq

优质
小牛编辑
124浏览
2023-12-01

(要求Workerman版本>=3.3.6)

安装:

  1. composer require react/zmq

示例:

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use WorkermanWorker;
  4. $worker = new Worker('text://0.0.0.0:6161');
  5. $worker->onWorkerStart = function() {
  6. global $pull;
  7. $loop = Worker::getEventLoop();
  8. $context = new ReactZMQContext($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: $msgn";
  16. });
  17. };
  18. Worker::runAll();

文档:

https://github.com/reactphp/zmq

注意:

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

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