Why broadcasting channel public not working? Laravel

余生长醉 提交于 2019-12-25 01:03:10

问题


I use laravel 5.3

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

The function like this :

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

Then it will call event

The event like this :

<?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);
});

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

Why is it not working?

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


回答1:


class CartNotificationEvent implements ShouldBroadcast is missing.



来源:https://stackoverflow.com/questions/45903508/why-broadcasting-channel-public-not-working-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!