【发布时间】:2019-01-19 16:51:51
【问题描述】:
我在 Laravel 5.5 中使用 laravel-echo-server 以及 Redis 和 vuejs 通过 websockets 广播事件。使用公共频道,它工作正常,事件可以正确广播到客户端。但是,当我将其更改为私有频道时,即使 channel.php 文件中的回调函数只返回 true 并且不包含任何身份验证逻辑,我也会面临身份验证问题。 我正在使用 Sentinel 身份验证包,我不知道这是否是问题所在。但正如我所说,当只是返回“真”时,身份验证问题仍然存在。
当我检查 laravel-echo-server 时,我看到有一条错误消息显示“无法通过身份验证,获得 http 代码 419”。
我了解到有些人面临同样的问题,他们得到的可能是 csrf-token 问题或私人频道仅适用于集成的身份验证包......等等。
我已在 window.Echo 配置中包含 csrf-token 标头,但没有结果。
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: {
headers: {
'X-CSRF-TOKEN': $('meta[name=csrf-token]).attr(content)
}
}
});
routes/channel.php
<?php
Broadcast::channel('adminNotify', function() {
return true;
});
为了让 id 为 1 的管理员能够接收事件,我实际上做了什么:
<?php
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
Broadcast::channel('adminNotify', function () {
return (int) Sentinel::check()->id === 1;
});
应用\事件\NotifyAdminEvent
<?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\Ad;
use Sentinel;
class NotifyAdminEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $ad;
public function __construct(Ad $ad)
{
$this->ad = $ad;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('adminNotify');
}
}
app.js
require('./bootstrap');
window.Vue = require('vue');
import Echo from 'laravel-echo';
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
laravel-echo-server.json
{
"authHost": "http://192.168.10.11",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "64fe4c095b0ffb30",
"key": "9e2bf37f9b3c8c88c3c5f5e207754f1d"
}
],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
Laravel 日志不记录任何内容,因此我发现很难知道发生了什么并解决问题。
【问题讨论】:
-
你可以回答你自己的问题,take a look。
-
我已回滚您的编辑。编辑您的帖子以添加(已解决)或添加解决方案是不合适的。如果你想分享你找到的解决方案,你可以在下面的空白处写一个答案,标题为你的答案。如果您只是想让问题消失,您可以使用标签下方的链接将其删除。更多信息请访问help center。
-
我不知道这对你有没有帮助! here
标签: laravel-5 vue.js websocket redis laravel-echo