ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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"; } } }); }); ```