【问题标题】:Pusher receives no eventsPusher 没有收到任何事件
【发布时间】:2016-11-09 17:08:38
【问题描述】:

我正在使用 laravel 和 pusher 向 pusher 发送事件消息。代码在我的控制器中,它是一个后控制器,在提交输入表单时触发。下面是我的代码。我究竟做错了什么?没有收到任何事件。 这是一个基于 ajax 调用路由的控制器。

$pusher = new Pusher( env('PUSHER_KEY'), env('PUSHER_SECRET'), env('PUSHER_APP_ID'), array( 'encrypted' => true ) );
$pusher->trigger( 'test_channel', 'my_event', 'hello world' );

【问题讨论】:

  • 你在dashboard.pusher.com的调试控制台中看到事件了吗?
  • 不,我没有看到任何事件
  • 我还没有在 Laravel 中使用过这个,但从我的阅读来看,你不应该使用 broadcast() 方法吗? laravel.com/docs/5.3/broadcasting#broadcasting-events
  • 我同意@Eric Tucker 的观点,即使用 Laravel 内置的 Pusher 和 Broadcasting 功能可能对你非常有用,除非你有充分的理由坚持使用 /vinkla/laravel-pusher 包。
  • @MohamedAthif 在这种情况下,事件一定不会到达 Pusher。我建议使用 tcpdump/Wireshark 来检查事件是否是通过网络发送的。

标签: javascript php laravel pusher


【解决方案1】:

我还假设您已正确设置 Pusher 帐户并且您的环境变量正确。

如果是这样,您可能需要确保使用正确的集群(默认值适用于美国,但例如在美国东海岸以外,必须明确定义集群)。

更新:

控制器代码:

<?php

namespace App\Http\Controllers;

use Vinkla\Pusher\Facades\Pusher;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class TestPusherController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function test(){
        $arr = array('test' => 'hello world 2') ;
        $pusher = new Pusher( env('PUSHER_KEY'), env('PUSHER_SECRET'), env('PUSHER_APP_ID'), array( 'encrypted' => true, 'cluster' => 'ap1' ) );
        $pusher::trigger( 'test_channel', 'my_event', $arr);

        return $arr;
    }

    public function shortenedTest(){
        $message = 'Hello world';
        Pusher::trigger('my-channel', 'my-event', ['message' => $message]);
    }

}

在网络路由中:

Route::get('testPusherController', 'TestPusherController@test');
Route::get('shortenedTestPusherController', 'TestPusherController@shortenedTest');

我已经按照 https://github.com/vinkla/laravel-pusher 中的设置步骤在 Laravel 5.3 上使用内置的 PHP 服务器并连接到欧盟服务器(我没有任何 Pusher 应用程序使用ap1)。

您会注意到对控制器中的编码进行了少量更改以获得正确的格式。您必须“使用”控制器上方的 Pusher 外观。

为了完整起见,我添加了一种更简洁的处理方式,您可以在 Config/pusher.php 文件中设置 Pusher 凭据,而无需为每次使用设置连接。这可以在控制器上的 shortedTest() 方法中看到。

<?php

return [

    'connections' => [
        'main' => [
            'auth_key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_CLUSTER')
            ],
            'host' => null,
            'port' => null,
            'timeout' => null,
        ],

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

    ],

];

【讨论】:

  • 集群是 AP1,我正在使用 Vinkler/pusher 包。绝对没有回应。但我可以触发与前端 javascript 的连接。只是不是控制器
  • 能够使用 Javascript 连接意味着您的 Pusher 应用程序应正确设置为侦听消息的代码。您的服务器端代码与此无关,必须正确设置才能广播消息。
  • 没问题。很高兴我能帮忙;-)
猜你喜欢
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2021-05-30
  • 2017-06-25
相关资源
最近更新 更多