laravel之pusher应用广播事件

安装请查看这个文章:https://www.jianshu.com/p/94a7f47e08f4

版本说明

composer指定版本可以查看
https://www.jianshu.com/p/ef31d9c9094b
laravel5.2
pusher版本
"pusher/pusher-php-server": "^2.2.1",
pusher laravel版本
"pusher/pusher-http-laravel": "~2.0"

安装

忽略怎么安装,
把创建pusher的配置文件读取到config下,
官方文档存在BUG,代码应该是

php artisan vendor:publish

config/app.php
引入

'providers' => [
    ..
  Vinkla\Pusher\PusherServiceProvider::class
]

别名那里不引入,有问题,后续有机会重写一个

搞定配置

配置文件说明

'connections' => [

        'main' => [
            'auth_key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [],
            'host' => null,
            'port' => null,
            'timeout' => null,
            //这里必须增加配置,表示
            'options'=>[
                'encrypted'=>false,//是否HTTPS
                'cluster'=>'ap1',//调用哪个api
            ]

        ],

        'alternative' => [
            'auth_key' => 'your-auth-key',
            'secret' => 'your-secret',
            'app_id' => 'your-app-id',
            'options' => [],
            'host' => null,
            'port' => null,
            'timeout' => null,
        ],

    ],

测试下是否可以使用

HTML文件,拿官方的来,订阅

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/4.1/pusher.min.js"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher('0a8d7355056b24b3dcd9', {
      cluster: 'ap1',
      encrypted: true
    });

    var channel = pusher.subscribe('my-channel');
    channel.bind('my-event', function(data) {
      alert(data.message);
    });
  </script>
</head>

laravel文件,发布
随便一个控制器,方法随便你,

$pusher = App::make('pusher');//因为直接用别名,刚才说了,引入会报错,所以用了这种方法注册。
$data['message'] = 'hello world';
$pusher->trigger('my-channel', 'my-event', $data);

浏览器访问就可以在html文件那里打开显示hell world


image.png

事件广播定义

创建事件定义,加入如下,这个怎么创建,请参考https://www.jianshu.com/p/68caa3df8bf5

'App\Events\PusherEvent' => [
           'App\Listeners\PusherEventListener',
       ]

执行生成

php artisan event:generate

envent继承ShouldBroadcast

class PusherEvent extends Event  implements ShouldBroadcast
{
    use SerializesModels;
    public $message, $id;

    /**
     * Create a new event instance.
     *
     * @return void
     */
     public function __construct($text, $id)
     {
       $this->message = $text;
       $this->id   = $id;
    }

    /**
     * Get the channels the event should be broadcast on.
     *这里是定义频道的名字
     * @return array
     */
    public function broadcastOn()
    {
         return ['channel'];
    }
    //事件名字,如果这里没有定义,则会直接以这个事件名字为事件名,例如:App\\Events\\PusherEvent;
    public function broadcastAs(){
      return ['my-event'];
    }
}

控制器调用

$str=str_random();
 $pusher = App::make('pusher');
event(new \App\Events\PusherEvent($str, '1'));//触发事件

html文件调用

 Pusher.logToConsole = true;
          var pusher = new Pusher('0a8d7355056b24b3dcd9', {
            cluster: 'ap1',
            encrypted: true
          });

          var channel = pusher.subscribe('channel');
          channel.bind('App\\Events\\PusherEvent', function(data) {
            alert(data.message);
          });

推荐阅读更多精彩内容