我正在使用laravel pusher和echo来播放一个事件,但它没有发生。
class NewOrderAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('public-test-channel');
}
}
以下是应该接收广播的代码。
listen(){
console.log("Listening...");
Echo.channel('public-test-channel')
.listen('NewOrderAdded', (e) => {
console.log("Event received");
});
}
这是bootstrap.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'xxxxxxxx',
cluster: 'ap1',
encrypted: true
});
控制器类
public function create(Request $request){
$order = new Order([
'user_id' => $request->get('user_id'),
'person_id' => json_encode($request->get('person_id')),
'date_of_booking' => $request->get('date_of_booking'),
'total_order_price' =>$request->get('total_order_price'),
'ticket_id' => json_encode($request->get('ticket_id')),
'timeslot' => $request->get('timeslot'),
'status' => 'pending',
'add_on_id' => json_encode($request->get('add_on_id'))
]);
$order->save();
event(new NewOrderAdded());
return response()->json(array('order_id'=>$order->id));
}