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

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

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

(要求Workerman版本>=3.3.6)

STOMP是一个通讯协议。它是支持大多数消息队列如RabbitMQ、Apollo等。

安装:

  1. composer require react/stomp

示例:

  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 $client;
  7. $loop = Worker::getEventLoop();
  8. $factory = new ReactStompFactory($loop);
  9. $client = $factory->createClient(array('vhost' => '/', 'login' => 'guest', 'passcode' => 'guest'));
  10. $client
  11. ->connect()
  12. ->then(function ($client) use ($loop) {
  13. $client->subscribe('/topic/foo', function ($frame) {
  14. echo "Message received: {$frame->body}n";
  15. });
  16. });
  17. };
  18. Worker::runAll();

文档:

https://github.com/reactphp/stomp

注意:

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

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