Redis没有在Laravel 5.1中接收广播事件

Ewa*_*ine 3 redis socket.io laravel laravel-5

我有以下事件:

<?php

namespace SixtyFiveContrib\Events;

use Auth;

use SixtyFiveContrib\Events\Event;
use SixtyFiveContrib\Models\Notification;

use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

/**
 * NotificationEvent 
 *
 */
class NotificationEvent extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $notification;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Notification $notification)
    {
        $this->notification = $notification;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['feed', 'updates'];
    }

    public function broadcastWith()
    {
        return ['notification' => $this->notification];
    }
}
Run Code Online (Sandbox Code Playgroud)

我在broadcast.php中使用Redis驱动程序

 'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
Run Code Online (Sandbox Code Playgroud)

然后我从官方文档获得节点应用程序,运行正常并连接到客户端:

var app = require('http').createServer(handler);
var io = require('socket.io')(app);

var Redis = require('ioredis');
var redis = new Redis();

app.listen(3000, function() {
    console.log('Server is running!');
});

function handler(req, res) {
    res.writeHead(200);
    res.end('Ayup.');
}

io.on('connection', function(socket) {
    //
});

redis.psubscribe('*', function(err, count) {
    console.log(err);
});

redis.on('pmessage', function(subscribed, channel, message) {
    message = JSON.parse(message);

    console.log(subscribed);
    console.log(channel);
    console.log(message);

    io.emit(channel + ':' + message.event, message.data);
});
Run Code Online (Sandbox Code Playgroud)

节点应用程序没有收到Redis的任何内容?如果我手动进入redis-cli并运行```PUBLISH feed'{event:"SixtyFiveContrib\Events\NotificationEvent"}',那么节点应用确实会收到该消息.

提前干杯!

jlm*_*mns 5

刚才有这个问题.

显然,广播事件使用QUEUE_DRIVER:

请参阅"队列先决条件":

在广播事件之前,您还需要配置并运行队列侦听器.所有事件广播都是通过排队的作业完成的,这样您的应用程序的响应时间就不会受到严重影响.

所以,要立即捕捉事件,你可以设置QUEUE_DRIVER=sync.
但这当然不建议,因为所有其他工作也会同步运行.
所以最好先设置一个合适的队列处理程序.