我的lumen应用程序中有一个名为“event1”的事件和一个事件侦听器“event1 listener”的事件。当event1被触发时,我需要将自定义消息发布到名为“channel1”的redis频道。我该怎么做?
事件1.php
<?php
namespace App\Events;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class Event1 extends Event implements ShouldBroadcast
{
/**
* Create a new event instance.
* @return void
*/
public function __construct() {
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn() {
return ['channel1'];
}
}
?>
event1listener.php文件
<?php
namespace App\Listeners;
use App\Events\Event1;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class Event1Listener {
/**
* Create the event listener.
*
* @return void
*/
public function __construct(){
}
/**
* Handle the event.
*
* @param Event1 $event
* @return void
*/
public function handle(Event1 $event) {
echo "What should I add here?";
}
}
?>
最佳答案
假设您已经配置了Redis
如果没有,请查看laravel documentation和lumen documentation
要将消息发布到频道,可以使用命令
public function handle(Event1 $event) {
Redis::publish('channel1', json_encode(['foo' => 'bar']));
}
关于php - 如何使用流明广播将消息发布到redis channel ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46823462/