gpt4 book ai didi

laravel - Nuxtjs + Laravel Echo + Laravel Passport + Laravel WebSockets 无法从私有(private)/存在 channel 获取事件

转载 作者:行者123 更新时间:2023-12-04 14:16:27 25 4
gpt4 key购买 nike

我正在使用与 Laravel 后端分开托管的 NuxtJS。为了对用户进行身份验证,我使用的是 Laravel Passport。我正在尝试使用 @nuxtjs/laravel-echopusher-js 为我的网站构建聊天功能。这是来自 beyondcode/laravel-websockets 包的自托管 websocket,与 php artisan websockets:serve 一起运行。作为一个例子,我正在使用这个 repo https://github.com/qirolab/Laravel-WebSockets-Chat-Example .唯一不同的是我使用的是 Laravel-Passport 和 NuxtJS

这是我的 nuxt.config.js 'buildModules` 部分

  buildModules: [
...
[
'@nuxtjs/laravel-echo',
{
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: false,
wsHost: process.env.WEBSOCKET_BASE_URL,
wsPort: 6001,
disableStats: true
}
]
],

echo: {
authModule: true,
connectOnLogin: true,
disconnectOnLogout: true
},

我在 Laravel 中的事件(App\Events\ChatMessageSent.php)如下所示:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\ChatMessages;

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

public $message;

public function __construct(ChatMessages $message)
{
$this->message = $message;
}

public function broadcastOn()
{
return new PrivateChannel('chat.' . $this->message->room->id);
}
}

目前我没有过滤用户,所以我的 channel.php 有这个条目:

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

事件将被分派(dispatch)为(我现在还没有使用 ..->toOthers()):

...
broadcast(new ChatMessageSent($message));
...

我的当前用户在我的 NuxtJS 应用程序中经过身份验证,并可通过 this.$auth.user

在我的前端文件中:

mounted:{
const channel = `chat.${this.roomID}`
this.$echo.private(channel).listen('ChatMessageSent', event => {
console.log(
'-------------------- This is happening!! -----------------------'
)
console.log(event)
this.messages.push(event.message)
})

但应用程序在所有 ChatMessageSent 事件上保持完全安静。根本没有控制台日志。如果我将其切换到公共(public) channel ,那么一切正常。我的猜测是身份验证有问题。在 websocket.php 配置文件中,我使用了 api 中间件

  */
'middleware' => [
'api',
Authorize::class,
],

有什么方法可以调试 websocket 上的身份验证过程?

顺便说一句,我在调试控制台中没有看到任何关于身份验证尝试的信息 http://localhost:8000/laravel-websockets

最佳答案

我很幸运找到了这篇文章https://medium.com/@mihkelallorg/laravel-react-jwt-tokens-and-laravel-echo-with-pusher-for-dummies-like-me-cafc5a09a1a1

要使上述方法起作用,第一步是(在 Laravel 端)在 config/app.php

中取消注释 App\Providers\BroadcastServiceProvider.php
'providers' => [    
...
App\Providers\BroadcastServiceProvider.php,
...
]

然后我们需要稍微调整一下 App\Providers\BroadcastServiceProvider.php 以使用 API 身份验证。将 Broadcast::routes() 替换为 Broadcast::routes(['middleware' => ['auth:api']]);

public function boot()
{
//Broadcast::routes();

Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}

现在我们只需要在 nuxt.config.js(NuxtJS 端)中添加一行来指定 Auth 端点。我的 BACKEND_BASE_URLhttp://localhost:8000 URL,由 php laravel serve

提供
buildModules: [
....
[
'@nuxtjs/laravel-echo',
{
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: false,
wsHost: process.env.WEBSOCKET_BASE_URL,
wsPort: 6001,
disableStats: true,
authEndpoint: process.env.BACKEND_BASE_URL + '/broadcasting/auth'
}
]
],

echo: {
authModule: true,
connectOnLogin: true,
disconnectOnLogout: true
},

现在私有(private) channel 正在使用自托管推送器

关于laravel - Nuxtjs + Laravel Echo + Laravel Passport + Laravel WebSockets 无法从私有(private)/存在 channel 获取事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59656054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com