为什么广播频道公众无法正常工作?拉拉韦尔 [英] Why broadcasting channel public not working? Laravel

查看:63
本文介绍了为什么广播频道公众无法正常工作?拉拉韦尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel 5.3

I use laravel 5.3

我这样制作routes/channels.php:

I make routes/channels.php like this :

<?php
Broadcast::channel('messages', function() {
    return true;
});

如果我输入数据购物车并单击提交,它将运行:

If I input the data cart and click submit, it will run this :

this.$http.post(window.BaseUrl + '/guest/add-notification', {cart_data: JSON.stringify(data)});

它将在控制器上调用功能

It will call function on the controller

这样的功能:

public function addNotification(Request $request){
    $input = $request->only('cart_data');
    $data = json_decode($input['cart_data'], true);
    event(new CartNotificationEvent($data));
}

然后它将调用事件

这样的事件:

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class CartNotificationEvent
{
    use InteractsWithSockets, SerializesModels;
    public $data;
    public function __construct($data)
    {
        $this->data = $data;
    }
    public function broadcastWith()
    {
        return [
            'message' => $this->data,
        ];
    }
    public function broadcastAs()
    {
        return 'newMessage';
    }
    public function broadcastOn()
    {
        return new Channel('messages');
    }
}

在客户端上,我喜欢这样:

On the client, I do like this :

Echo.channel('messages')
.listen('.newMessage', (message) => {
    console.log('test')
    console.log(message);
});

执行完所有代码后,我在控制台上检查,console.log没有显示

When all the code is executed, I check on the console, the console.log not display

为什么不起作用?

如果我看到自己编写的全部代码,则表明该过程是正确的

If I see the whole code that I make, it seems the process is correct

推荐答案

class CartNotificationEvent implements ShouldBroadcast丢失.

这篇关于为什么广播频道公众无法正常工作?拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆