🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## redis 事件订阅发布 ``` <?php use Swoole\Coroutine; use Swoole\Coroutine\Redis as SwooleCoroutineRedis; // 创建 Swoole 协程 Coroutine\run(function () { // 协程1:发送 Redis 事件 Coroutine::create(function () { $publisher = new SwooleCoroutineRedis(); $publisher->connect('127.0.0.1', 6379); while (true) { $message = time(); // 示例:发送当前时间作为消息 $publisher->publish('channel', $message); echo "Sent message: $message\n"; Coroutine::sleep(1); // 每秒发送一次事件 } }); //创建 Redis 订阅客户端 //协程2:订阅 Redis 事件 Coroutine::create(function () { $subscriber = new SwooleCoroutineRedis(); $subscriber->connect('127.0.0.1', 6379); // 订阅 Redis 事件 $subscriber->subscribe(['channel']); while (true) { $message = $subscriber->recv(); // 接收消息 if ($message !== false) { list($a,$channel, $msg) = $message; echo "Received message from channel:$channel: $msg\n"; } } }); }); ```