【问题标题】:Pusher and Laravel 5.3 Event BroadcastingPusher 和 Laravel 5.3 事件广播
【发布时间】:2016-11-25 18:54:01
【问题描述】:

尝试将 Laravel 5.3 与 pusher 一起使用,但在我的代码中似乎无法正常工作。

我的 .env 是正确的

PUSHER_APP_ID= myappid
PUSHER_KEY= mykey
PUSHER_SECRET= mysecret

这是我在 broadcasting.php 中的“推送器”配置

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

我创建了一个活动,在这里

<?php

namespace App\Events;

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 ProposalEvent implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

    public $data;

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

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

我的 javascript

Pusher.logToConsole = true;

var pusher = new Pusher("mykey", {
  cluster: 'eu',
  encrypted: true
});
var channel = pusher.subscribe('test-channel');
channel.bind('App\\Events\\ProposalEvent', function(data) {
  alert(data);
});

最后在我看来

event(new App\Events\ProposalEvent('some data'));

不幸的是,这对我不起作用,但是当我像这样使用 pusher->trigger 时,没有事件,它工作正常,并且我在 pusher 调试控制台中看到消息

$options = array(
  'cluster' => 'eu',
  'encrypted' => true
);
$pusher = new Pusher(
  'mykey',
  'mysecret',
  'myid',
  $options
);

$data['message'] = 'some data';
$pusher->trigger('test-channel', 'my-event', $data);

我在 Laravel 文档和其他资源中搜索了解决方案。在stackoverflow中有同样问题的问题,但没有回复。如果有人能帮助我,我将不胜感激,因为我几天都找不到解决方案

【问题讨论】:

    标签: laravel events websocket pusher broadcasting


    【解决方案1】:

    我陷入了同样的境地,发现我没有使用队列!

    在文档中说

    在广播事件之前,您还需要配置和运行队列监听器。所有事件广播都是通过排队作业完成的,因此您的应用程序的响应时间不会受到严重影响。

    我之前删除了 config/queue.php 文件,因为我认为我没有使用它。也许你和我做同样的事情,或者队列有问题。

    【讨论】:

      【解决方案2】:

      尝试直接通过 config/broadcasting.php 传递推送者凭据

      它对我有用。

      'default' =>  'pusher',
      'connections' => [
      
          'pusher' => [
              'driver' => 'pusher',
              'key' => '***',
              'secret' => '**',
              'app_id' => '**',
              'options' => [
              ],
          ],
      ],
      enter code here
      

      【讨论】:

        【解决方案3】:

        在视图中触发事件是一个错误,当页面加载时,首先执行 php,然后加载 js,这意味着您在 js listner 之前用 php 触发了事件,这将显示一个错误,这可能是第一种情况 第二种情况是你没有下载https://curl.haxx.se/docs/caextract.html 如果你确实去了 php.ini 并且在 [curl] 你会发现类似 curl-info 的东西取消注释它 并正确设置 culr 文件的路径

        【讨论】:

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