Laravel如何利用pusher推送消息

小編給大家分享一下Laravel如何利用pusher推送消息,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)公司公司2013年成立,先為剛察等服務(wù)建站,剛察等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為剛察企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一.注冊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;
    }
 
    /**
     * 指定廣播頻道(對應(yīng)前端的頻道)
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['my-channel'];
    }
 
    /**
     * 指定廣播事件(對應(yīng)前端的事件)
     * @return string
     */
    public function broadcastAs()
    {
        return 'my-event';
    }
 
    /**
     * 獲取廣播數(shù)據(jù),默認是廣播的public屬性的數(shù)據(jù)
     */
    public function broadcastWith()
    {
        return ['info' => $this->info];
    }
}

2.廣播事件,并不需要監(jiān)聽器;廣播事件需要繼承接口ShouldBroadcast

四.廣播

1.觸發(fā)事件

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提交數(shù)據(jù),所以你需要配置證書;否則提交會失敗

2.如果不配置證書,則需要設(shè)置curl的CURLOPT_SSL_VERIFYPEERCURLOPT_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如何利用pusher推送消息”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:Laravel如何利用pusher推送消息
網(wǎng)頁鏈接:http://muchs.cn/article4/pdppoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)企業(yè)建站、網(wǎng)站維護小程序開發(fā)、搜索引擎優(yōu)化Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司