2017-08-27 121 views
0

我用laravel 5.3爲什麼廣播頻道公衆無法使用? Laravel

我做路由/ channels.php這樣的:

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

如果我輸入的數據車,然後點擊提交,它將運行此:

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

它會調用控制器上的功能

像這樣的功能:

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

在客戶端,我這樣做:

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

當執行所有的代碼,我檢查控制檯,console.log不顯示

爲什麼不起作用?

如果我看到整個代碼,我做,似乎這個過程是正確的

回答

1

class CartNotificationEvent implements ShouldBroadcast丟失。

+0

太好了。有用。非常感謝 –