Laravel pusher广播不发送事件

时间:2016-09-08 19:25:04

标签: php laravel pusher laravel-5.3

我的应用程序有一个非常不寻常的广播问题,我已将它同步到Dropbox和我的笔记本电脑上我可以运行它并将事件发送到推送服务器,但是,在我的桌面上它没有。我使用相同版本的XAMPP服务器,相同的迁移等任何人都可以提出任何建议?代码如下。

应用\活动\ TodoCreated.php

<?php

namespace App\Events;

use App\Models\Todo;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class TodoCreated extends Event implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $todo;

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

    /*public function broadcastWith()
    {

    }*/

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return ['team.' . $this->todo->team_id . '.todos'];
    }
}

配置/ broadcasting.php

'connections' => [

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_KEY'),
        'secret' => env('PUSHER_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => 'eu',
            'encrypted' => true
        ],
    ],

.ENV

BROADCAST_DRIVER=pusher
...
PUSHER_APP_ID=<app_id>
PUSHER_KEY=<key>
PUSHER_SECRET=<secret>
  • 我使用event(new App\Events\TodoCreate($todo))点击活动,其中$todoApp\Models\Todo
  • 的实例
  • 我在config / app.php文件中启用了BroadcastServiceProvider
  • 我符文php artisan queue:work --timeout=0并看到事件已注册并成功运行但我的推送调试面板上没有收到任何内容

1 个答案:

答案 0 :(得分:2)

我编码测试推送器广播发送事件,没关系。我使用Laravel 5.1。请查看以下代码

<强> 1。文件配置(或配置文件.env)broadcasting.php

'default' => env('BROADCAST_DRIVER', 'pusher'),
    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY','xxxxxxxxxxxxxxxxx'),
            'secret' => env('PUSHER_SECRET','xxxxxxxxxxxxxxxxx'),
            'app_id' => env('PUSHER_APP_ID','xxxxxx'),
            'options' => [
                //
            ],
        ],

<强> 2。路由器

Route::get('/', function () {
    return view('welcome');
});
Route::get('/broadcast', function() {
    event(new \App\Events\TestEvent('Broadcasting in Laravel using Pusher!'));

});

3。的 TestEvent.php

namespace App\Events;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class TestEvent implements ShouldBroadcast
{
    public $text;

    public function __construct($text)
    {
        $this->text = $text;
    }

    public function broadcastOn()
    {
        return ['test-channel'];
    }
}

<强> 4。作曲家

"require": {...,"pusher/pusher-php-server": "^2.2",}

5。的 welcome.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>

        <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                display: table;
                font-weight: 100;
                font-family: 'Lato';
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }

            .title {
                font-size: 96px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">Laravel 5</div>
            </div>
        </div>
    </body>
    <script src="//js.pusher.com/3.0/pusher.min.js"></script>
    <script>
        var pusher = new Pusher("xxxxxxxxxxxxxxxxxxxxxxxxxx")
        var channel = pusher.subscribe('test-channel');
        channel.bind('App\\Events\\TestEvent', function(data) {
            console.log(data.text);
        });
    </script>
</html>

我希望帮助你!