最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Laravel利用pusher推送消息的方法详解

来源:懂视网 责编:小采 时间:2020-11-03 18:19:03
文档

Laravel利用pusher推送消息的方法详解

Laravel利用pusher推送消息的方法详解:一.注册pusher1.注册https://pusher.com/2.获取key,密匙,app_id等 二.配置pusher1.安装pushercomposer require pusher/pusher-php-server2.配置config/broadcasting.php'default' => env(
推荐度:
导读Laravel利用pusher推送消息的方法详解:一.注册pusher1.注册https://pusher.com/2.获取key,密匙,app_id等 二.配置pusher1.安装pushercomposer require pusher/pusher-php-server2.配置config/broadcasting.php'default' => env(

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 AppEvents;
 
use AppEventsEvent;
use IlluminateQueueSerializesModels;
use IlluminateContractsBroadcastingShouldBroadcast;
 
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

四.广播

1.触发事件

event(new AppEventsPusherEvent('测试'));

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_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框架相关技术文章,请访问laravel教程栏目!

文档

Laravel利用pusher推送消息的方法详解

Laravel利用pusher推送消息的方法详解:一.注册pusher1.注册https://pusher.com/2.获取key,密匙,app_id等 二.配置pusher1.安装pushercomposer require pusher/pusher-php-server2.配置config/broadcasting.php'default' => env(
推荐度:
标签: 消息 laravel pusher
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top