Pusher 没有收到任何事件

Pusher receives no events

我正在使用 laravel 和推送器向推送器发送事件消息。代码在我的控制器中,它是一个 post 控制器,在提交输入表单时触发。下面是我的代码。我究竟做错了什么?没有收到任何事件。 这是一个 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' );

我还假设您已正确设置您的 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 服务器和连接,在 vinkla/pusher 的全新安装中完成了这项工作到欧盟服务器(我目前没有任何使用 ap1 的 Pusher 应用程序)。

您会注意到控制器中的编码有少量更改以获得正确的格式。您必须 'use' 控制器上方的 Pusher facade。

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

<?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,
        ],

    ],

];