【问题标题】:Laravel pusher Broadcasting not sending eventsLaravel pusher 广播不发送事件
【发布时间】:2016-09-08 19:25:04
【问题描述】:

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

App\Events\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'];
    }
}

config/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 并看到事件已注册并成功运行,但在我的推送调试面板上没有收到任何内容

【问题讨论】:

    标签: php laravel pusher laravel-5.3


    【解决方案1】:

    我编码测试推送器广播发送事件,没关系。我使用 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>
    

    希望能帮到你!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2017-07-04
    • 2019-01-22
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多