[英]Laravel-echo-server and Socket.io not firing command even though it's listening to channel
[英]Socket.io is not listening in laravel echo server
我正在尝试使用 socket.io 与 laravel 回显服务器实现聊天功能,但无法收听事件
这是我下面的代码
引导程序.js
import Echo from 'laravel-echo'
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://127.0.0.1:6001',
auth: {
headers: {
Authorization : 'my_bearer token'
}
}
});
应用程序.js
require('./bootstrap');
import Vue from 'vue';
const app = new Vue({
el: '#app',
data: {
message:'',
chat:{
message:[]
}
},
mounted() {
console.log('here i can able to subscribe private channel')
Echo.private('user.1')
.listen('App\\Events\\SendChatMessage', (e) => {
console.log('trying first method', e); //unable to listen
})
.listen('SendChatMessage', (e) => {
console.log('trying second method', e); //unable to listen
});
}
});
事件 SendChatMessage.php
public function broadcastOn()
{
return new PrivateChannel('user.1');
}
.env
BROADCAST_DRIVER=redis
QUEUE_CONNECTION=redis
laravel-echo-server.json
{
"authHost": "http://127.0.0.1:8000",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "3f4e63443c11bb0d",
"key": "59e850fecad73c2025c6bab9f2b9ce06"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "127.0.0.1"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "http://localhost:80",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
我的命令提示符
php 工匠队列:听
[2021-03-30 23:11:42][feQuvUDNmDg8oEUR6mwXZDQOvzpjLCbZ] Processing: App\Events\SendChatMessage
[2021-03-30 23:11:42][feQuvUDNmDg8oEUR6mwXZDQOvzpjLCbZ] Processed: App\Events\SendChatMessage
laravel-echo-server 启动
[11:10:28 PM] - 74J_fL-s4HNFXv3xAAAP authenticated for: private-user.1
[11:10:28 PM] - 74J_fL-s4HNFXv3xAAAP joined channel: private-user.1
Channel: messer_database_private-user.1
Event: App\Events\SendChatMessage
通道.php
Broadcast::channel('user.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
广播服务提供者
public function boot()
{
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
版本
Laravel: ^8.12
predis/predis": "^1.1
Package.json
laravel-echo": "^1.10.0
socket.io-client": "^4.0.0
vue": "^2.6.12
任何帮助将不胜感激
您的前端代码和事件代码是正确的。 如您所见,用户 id 已通过身份验证,并且正在广播。
但是,该事件不会广播到添加用户身份验证的同一频道,因此用户永远不会收到它。 在您支持的代码中的某处,您的频道以messer_database
为前缀。
您应该检查您的 redis 和广播配置,然后删除前缀或让广播理解前缀。
您必须删除 Redis 前缀messer_database_
检查config/database.php
并检查前缀
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
]
]
使用“socket.io-client”:“^2.4.0”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.