一.註冊pusher
#1.註冊
##https:// pusher.com/2.取得key,密匙,app_id等#
二.設定pusher
#1.安裝pusher
composer require pusher/pusher-php-server
2.設定config/broadcasting.php
'default' => env('BROADCAST_DRIVER', 'pusher'), .... 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_KEY'), 'secret' => env('PUSHER_SECRET'), 'app_id' => env('PUSHER_APP_ID'), 'options' => [ 'cluster' => 'ap1', 'encrypted' => true ], ], .....
三.建立事件1.程式碼如下:
<?php namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class PusherEvent extends Event implements ShouldBroadcast { use SerializesModels; public $info; /** * PusherEvent constructor. */ public function __construct($info) { $this->info = $info; } /** * 指定广播频道(对应前端的频道) * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { return ['my-channel']; } /** * 指定广播事件(对应前端的事件) * @return string */ public function broadcastAs() { return 'my-event'; } /** * 获取广播数据,默认是广播的public属性的数据 */ public function broadcastWith() { return ['info' => $this->info]; } }2.廣播事件,並不需要監聽器;廣播事件需要繼承介面ShouldBroadcast
#
四.廣播
event(new \App\Events\PusherEvent('测试'));2.前端程式碼
<!DOCTYPE html> <head> <title>Pusher Test</title> <script src="https://js.pusher.com/4.0/pusher.min.js"></script> <script> // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var pusher = new Pusher('XXX', { cluster: 'ap1', encrypted: true }); var channel = pusher.subscribe('my-channel'); channel.bind('my-event', function(data) { alert(data.info); }); </script> </head>ps:######1.pusher使用curl向###https://pusher. com###提交資料,所以你需要設定憑證;否則提交會失敗######2.如果不設定憑證,則需要設定curl的###CURLOPT_SSL_VERIFYPEER###和###CURLOPT_SSL_VERIFYHOST##### ######在###vender/pusher/pusher-php-server/lib/Pusher.php###中的trigger的#########curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value );#########下面增加:###
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);###更多laravel框架相關技術文章,請造訪###laravel教學###專欄! ###
以上是Laravel利用pusher推播訊息的方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!