0

我在用着

  • 拉拉维尔:8.54
  • Laravel 圣所:2.11
  • Pusher PHP 服务器:7.0
  • NuxtJs:2.15.7
  • pusher-js:7.0.4
  • laravel 回声:1.11.3

事件类

    <?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RaffleEntriesCreated implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'private-raffle-entry';
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('RaffleEntry.'.$this->userid);
    }
}

在我的频道文件上

Broadcast::channel('RaffleEntry.{id}',  function ($user, $id) {
    return (int) $user->id === (int) $id;
});

在 nuxt 插件上

       import Echo from 'laravel-echo';
        window.Pusher = require('pusher-js');
        export default ({ app }, inject) => {
          const echo = new Echo({
              broadcaster: 'pusher',
              key: "mykey",
              authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
              encrypted: true,
              forceTLS: true,
              cluster: 'ap2',
              authorizer: (channel, options) => {
                return {
                    authorize: (socketId, callback) => {
                        app.$axios.$post('/api/broadcasting/auth', {
                            socket_id: socketId,
                            channel_name: channel.name
                        })
                        .then(response => {
                            callback(false, response.data);
                            console.log('from oklugib ');
                        })
                        .catch(error => {
                            callback(true, error);
                        });
                    }
                };
              }
          });
          inject(
    
    'echo', echo)
    }

客户代码

 this.$echo.private(`RaffleEntry.${this.$auth.user.id}`)
        .listen('.private-raffle-entry', (e) => {
            console.log(e);
        });

我可以在推送器仪表板中看到广播消息,也可以看到客户端连接,用户可以完美验证,问题是它在验证后没有订阅。请在订阅前查看这些请求对用户进行身份验证的屏幕截图。你有什么想法我可以解决这个问题吗?

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

0

好的,解决了这个问题,在我的 nuxt 插件文件中,您只需返回responsevar not的授权选项respose.data

import Echo from 'laravel-echo';
        window.Pusher = require('pusher-js');
        export default ({ app }, inject) => {
          const echo = new Echo({
              broadcaster: 'pusher',
              key: "mykey",
              authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
              encrypted: true,
              forceTLS: true,
              cluster: 'ap2',
              authorizer: (channel, options) => {
                return {
                    authorize: (socketId, callback) => {
                        app.$axios.$post('/api/broadcasting/auth', {
                            socket_id: socketId,
                            channel_name: channel.name
                        })
                        .then(response => {
                            callback(false, response);
                     
                        })
                        .catch(error => {
                            callback(true, error);
                        });
                    }
                };
              }
          });
          inject(
    
    'echo', echo)
    }
于 2022-02-06T07:10:54.673 回答