0

我们使用 Pusher 来广播 Laravel 通知事件。

我在授权私人频道上的用户时遇到问题。auth 'key:signature' 被返回,但格式不同。

在此处输入图像描述

返回的对象看起来像我上传的图像

认证:“:xoxoxoxoxox”

根据 pusher 文档,它应该看起来像这样

{“身份验证”:“49e26cb8e9dde3dfc009:a8cf1d3deefbb1bdc6a9d1547640d49d94b4b512320e2597c257a740edd1788f”,}

我的推手js

    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://js.pusher.com/4.1/pusher.min.js"></script>
    <script>
Pusher.logToConsole = true;

const socket = new Pusher('17d5cedc0062ecd557ff', {
    encrypted: true,
    disableStats: true,
    authEndpoint: '/broadcasting/auth',
    auth: {
        headers: {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
        }
    },
    enabledTransports: ['ws', 'xhr_streaming','xhr_polling','sockjs'],
});

var channel = socket.subscribe('private-App.User.' + {{Auth::user()->id}});

var socketId = null;
socket.connection.bind('connected', function() {
    socketId = socket.connection.socket_id;
});

channel.bind('Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', function (data) {
    console.log(data);
});

广播.php

返回 [

/*
|--------------------------------------------------------------------------
| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "redis", "log", "null"
|
*/

'default' => env('BROADCAST_DRIVER', 'pusher'),

/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/

'connections' => [

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('17d5cedc0062ecd557ff'),
        'secret' => env('fef9521f13ca7bb4675a'),
        'app_id' => env('321223'),
        'options' => [
            'cluster' => 'mp1',
            'encrypted' => true
        ],
    ],

.env

    BROADCAST_DRIVER=pusher
    PUSHER_APP_ID=321223
    PUSHER_APP_KEY=17d5cedc0062ecd557ff
    PUSHER_APP_SECRET=fef9521f13ca7bb4675a

app.js 或 Echo 看起来像这样

    import Echo from "laravel-echo"

    window.Echo = new Echo({
        broadcaster: 'pusher',
        key: '17d5cedc0062ecd557ff'
     });

   Echo.private('App.User.${userId}')
        .notification((notification) => {
        console.log(notification.type);
    });

Laravel 5.3、Laravel-Echo、PusherJS 和 pusher-http-php 库

4

2 回答 2

2

我在那里看到了一些问题,

尝试将此文件: broadcasting.php 更改 为:

| Default Broadcaster
|--------------------------------------------------------------------------
|
| This option controls the default broadcaster that will be used by the
| framework when an event needs to be broadcast. You may set this to
| any of the connections defined in the "connections" array below.
|
| Supported: "pusher", "redis", "log", "null"
|
*/

'default' => env('BROADCAST_DRIVER', 'pusher'),

/*
|--------------------------------------------------------------------------
| Broadcast Connections
|--------------------------------------------------------------------------
|
| Here you may define all of the broadcast connections that will be used
| to broadcast events to other systems or over websockets. Samples of
| each available type of connection are provided inside this array.
|
*/

'connections' => [

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => 'mp1',
            'encrypted' => true
        ],
    ],

看起来你正试图以不好的方式获取环境变量,

于 2017-05-09T18:05:41.017 回答
1

默认路由广播/身份验证无法检索到合适的响应,因此您可以像这样添加自定义 authEndPoint

在 web.php 中

Route::get('pusher/auth', 'PusherController@pusherAuth');

并制作 PusherController :

class PusherController extends Controller
{
    /**
     * Authenticates logged-in user in the Pusher JS app
     * For presence channels
     */
    public function pusherAuth()
    {

        $user = auth()->user();

        if ($user) {
            $pusher = new Pusher('auth-key', 'secret', 'app_id');
            $auth= $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id'));
            $callback = str_replace('\\', '', $_GET['callback']);
            header('Content-Type: application/javascript');
            echo($callback . '(' . $auth . ');');
            return;
        }else {
            header('', true, 403);
            echo "Forbidden";
            return;
        }
    }
}

应以正​​确的格式给出响应,我们必须自定义此表单的答案

        $auth= $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id'));
        $callback = str_replace('\\', '', $_GET['callback']);
        header('Content-Type: application/javascript');
        echo($callback . '(' . $auth . ');');
于 2019-03-05T05:14:00.967 回答