Laravel 事件广播不适用于推送器

标签 laravel events pusher broadcasting

我在我的项目中使用了 pusher。我按照 laravel 文档配置广播。当我解雇我的事件推送器时,它对我不起作用。但是当我从推送器控制台发送数据时,推送器会收到此数据。
我也尝试vinkla/pusher。它的工作正常,但 Laravel 事件广播不起作用。
请帮我。

这是我的 TestEvent.php代码

namespace Factum\Events;

use Factum\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class TestEvent implements ShouldBroadcast
{
    use SerializesModels;

    public $text;

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

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['test-channel'];
    }
}

最佳答案

我遇到了类似的问题,并逐步解决了问题。
我假设您正在运行 Laravel 5.3。
以下是可能有帮助的分步演练:

  • 检查您的配置文件。在 config\broadcasting.php :
    'connections' => [
       'pusher' => [
         'driver' => 'pusher',
         'key' => env('PUSHER_KEY'),
         'secret' => env('PUSHER_SECRET'),
         'app_id' => env('PUSHER_ID'),
         'options' => [
                             'cluster' => 'eu',
                             'encrypted' => true,
                          // 'host' => 'api-eu.pusher.com'
                          // 'debug' => true,
                      ],
        ],
    ],
    
  • 在您的 web.php 中创建用于测试的路由文件:
    Route::get('/broadcast', function() {
        event(new \Factum\Events\TestEvent('Sent from my Laravel application'));
    
        return 'ok';
    });
    
  • 在您的 TestEvent.php事件文件,您应该添加此方法以指定您的事件名称:
    /**
      * The event's broadcast name.
      *
      * @return string
      */
    public function broadcastAs()
    {
        return 'my_event';
    }
    
  • 打开您的 Pusher dashboard并转到调试控制台。
    保持页面打开,以便您可以注意到是否收到了来自应用程序的成功请求。
  • 启动或重新启动您的队列工作器。这一步可以成就或破坏一切。如果您将 Mysql 表用于队列,我将假设您已经设置了队列所需的“jobs”和“failed_jobs”数据库表。
    另一个重要的元素是 worker ——队列处理器。
    如果没有事件工作人员运行来处理您的队列,作业 (TestEvent) 将“保留”在工作表中,这意味着工作处于挂起状态,并且在事件工作人员开始处理队列之前不会再发生任何事情。
    您可以像这样启动工作程序:
    www@yourmachine# PHP artisan queue:work --tries=3
    
  • 现在,您已经拥有了一切可以调用“http://your-app.laravel/broadcast”并检查您的推送器调试控制台是否有响应。

  • 可选步骤:
    如果仍然缺少某些内容,您可以像这样调试应用程序与 Pusher 的交互:
    在您的测试 route 尝试这样做:
    Route::get('/broadcast', function() {
        
        // New Pusher instance with our config data
        $pusher = new \Pusher(
            config('broadcasting.connections.pusher.key'),
            config('broadcasting.connections.pusher.secret'),
            config('broadcasting.connections.pusher.app_id'),
            config('broadcasting.connections.pusher.options')
        );
            
        // Enable pusher logging - I used an anonymous class and the Monolog
        $pusher->set_logger(new class {
               public function log($msg)
               {
                     \Log::info($msg);
               }
        });
            
        // Your data that you would like to send to Pusher
        $data = ['text' => 'hello world from Laravel 5.3'];
            
        // Sending the data to channel: "test_channel" with "my_event" event
        $pusher->trigger( 'test_channel', 'my_event', $data);
            
        return 'ok'; 
    });
    
    我希望它也对你有用!
    祝你编码愉快! ;)

    关于Laravel 事件广播不适用于推送器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37854005/

    相关文章:

    php - Laravel:使用数据透视表时在查询级别订购数据?

    laravel - PhpWord TemplateProcessor 克隆表行并在其中插入信息

    html - 如何调整弹出日期和标题的显示?

    用于 jQuery 未请求的 POST 数据的 jQuery 事件监听器

    Javascript "this"通过不同的范围

    pusher - 来自多个推送 channel 的实时更新

    php - 从 Laravel 数据库中检索所有父/子记录(分层数据)

    java - Swing:在鼠标悬停时显示/隐藏按钮

    laravel-5 - 未找到类 'Pusher'

    push-notification - Pusher:mysql数据变化时触发pusher