gpt4 book ai didi

Laravel Echo 私有(private) channel 未收听

转载 作者:行者123 更新时间:2023-12-01 23:04:34 25 4
gpt4 key购买 nike

我正在尝试创建一个实时聊天应用程序。我使用了 Laravel Echo、Redis 和 socket-io。

但是 laravel-echo 没有监听私有(private)广播聊天 channel 。

App\Events\MessagePushed

public function broadcastAs()
{
return 'new.message';
}

public function broadcastOn()
{
return new PrivateChannel("application-chat-{$this->message->job_application_id}");
}

routes/channels.php

Broadcast::channel('application-chat-{job_application_id}', function ($user, $job_application_id) {
return auth()->check();
});

存储消息资源方法

public function store(StoreFormRequest $request, JobApplication $application)
{
$request->merge([
'sender_id' => $request->user()->id,
]);

$message = $application->messages()->create($request->all())
->load(['sender', 'receiver']);

event(new MessagePushed($message));

return response()->json([
'message' => $message,
]);
}

收听私有(private) channel

window.Echo.private(`application-chat-${this.messageApplication}`)
.listen('.new.message', (data) => {
console.clear();
console.log('Got event...', data);
});

最佳答案

希望它有助于调试:

Laravel echo 服务器配置:

{
"authHost": "domain.local",
"authEndpoint": "/broadcasting/auth",
"database": "redis",
"databaseConfig": {
"redis": {}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}

php artisan 队列:工作:

检查队列是否发送事件

enter image description here

laravel echo 服务器

检查用户是否加入 channel 以及 echo 服务器发送事件

enter image description here

Laravel Echo:

可能是 broacastAs 有问题。尽量不要使用它。不要忘记将命名空间属性添加到配置中。

window.Echo = new Echo({
namespace: 'App.Events',
broadcaster: 'socket.io',
host: window.location.protocol + '//' + window.location.hostname + ':6001',
auth: {
headers: {
Authorization: //auth
}
}
});

事件名称 = 事件类名称

window.Echo.private(`application-chat-${this.messageApplication}`)
.listen('MessagePushed', (data) => {
console.log(data);
});

关于Laravel Echo 私有(private) channel 未收听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920280/

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