![](/img/trans.png)
[英]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.