【问题标题】:Laravel Broadcast Private Channel doesnt workLaravel 广播私人频道不起作用
【发布时间】:2021-07-15 09:06:22
【问题描述】:

我正在尝试使用 Laravel WebSockets 作为 socket 服务器Laravel EchoEvents 进行一些实时推送通知.

当我在Channel 上执行此操作时,它工作正常,但现在我希望它发送到私人频道

Channel('reservation.' . $this->random_key);

PrivateChannel('reservation.' . $this->random_key);

NewReservationEvent.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewReservationEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    public $random_key;

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

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

    public function broadcastAs()
    {
        return 'reservation.event';
    }
}

routes/channels.php

<?php

use Illuminate\Support\Facades\Broadcast;

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

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

Broadcast::channel('reservation.{random_key}', function ($user, $random_key){
    return true;
});

routes/web.php

Route::get('/sendEvent', function () {
    $superAdmins = User::where('role', 'Super')->get();

    foreach ($superAdmins as $superAdmin) {
        $message = 'Reservation added by';
        event(new NewReservationEvent($message, $superAdmin));
    }
});

BroadcastServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;

class BroadcastServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Broadcast::routes(['middleware' => ['auth', 'checkRole:Super,Admin,Customer']]);

        require base_path('routes/channels.php');
    }
}

这是我认为的Echo

Echo.private('reservation.{{ auth()->user()->random_key }}')
    .listen('.reservation.event', (e) => {
        console.log(e.message);

        $("#refreshThisDropdown").load(window.location.href + " #refreshThisDropdown");
        $("#refreshThisDropdown").load(" #refreshThisDropdown > *");

        toastr.success(e.message, "Hello there");
    })

Channel 模式下一切正常,我想将其设为 private 以提高安全性。

【问题讨论】:

  • 您在会话中的基础身份验证到基于令牌的身份验证?
  • 是的,我用 auth 保护它。并创建检查角色中间件来访问一些路由
  • 我发布了我的答案你能检查你是否设置了这个然后/auth api 将自动点击授权 websocket 私人频道

标签: php laravel websocket pusher


【解决方案1】:

我遇到了同样的问题,我通过在 Channel.php 中添加 Gurad 解决了这个问题

喜欢这个

routes/channels.php

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
}, ['guards' => ['web', 'auth']]);

Broadcast::channel('reservation.{random_key}', function ($user, $random_key){
    return true;
}, ['guards' => ['web', 'auth']]);

【讨论】:

  • 我尝试了这个,但很遗憾,它不起作用。如果你想查看我的完整代码,我有 github repo。我在我的 /larave-websockets 上得到了其他线索。每当我将频道放到私人频道时,它不会订阅,它只显示连接 799386905.254837412 来源:127.0.0.1。正常吗?
  • 订阅私人频道 laravel 需要授权哪个 laravel 自动调用 api 那里你得到了问题
  • 我认为我离解决问题越来越近了,所以我改变 Broadcast::channel('reservation.{random_key}', function ($user, $random_key){ return true; }, ['middleware' => ['auth', 'checkRole:Super,Admin,Customer']]);这个公共函数 boot() { Broadcast::routes();需要 base_path('routes/channels.php'); }
  • 我的私人频道现在订阅了活动,但现在我在控制台 app.js:21789 POST 127.0.0.1:8000/broadcasting/auth 403 上遇到错误(禁止)。但是如果我在广播路由中添加中间件身份验证检查角色,我的私人频道就无法再订阅了。
  • 然后你需要玩guards 右添加guards 然后它会连接否则你会得到403
猜你喜欢
  • 2021-11-15
  • 2021-06-01
  • 2020-03-24
  • 2018-03-03
  • 2021-11-08
  • 2018-02-23
  • 2023-03-17
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多