如何解决Laravel-echo 接收事件但不加入频道侦听器
首先,我有一个实时聊天应用程序(Vue 作为前端,Laravel 作为后端)
在 localhost 上它可以完美运行,但在生产中却不能。
我注意到我确实收到了活动,但我一直无法加入任何频道。
这是所有信息,我希望您对如何解决它有任何想法。谢谢!
Main.js(laravel-echo/socket.io 连接):
import Echo from "laravel-echo"
window.io = require('socket.io-client')
window.Echo = new Echo({
broadcaster: 'socket.io',host: 'https://example.com/socket.io',auth: {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
}
});
聊天组件(监听器):
window.Echo.private(`chat.${this.user.id}`)
.listen('NewMessage',(e) => {
console.log(e.message);
});
Nginx 配置:
server {
server_name example.com www.example.com;
root /var/www/html/example/api/public/;
index index.html index.PHP;
location / {
root /var/www/html/example/web/dist/;
try_files $uri $uri/ /index.html;
}
location /socket.io {
proxy_pass http://localhost:6001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
try_files $uri $uri/ /index.PHP$is_args$args;
}
location ~ \.PHP$ {
include snippets/fastcgi-PHP.conf;
fastcgi_pass unix:/run/PHP/PHP8.0-fpm.sock;
}
}
Laravel-echo 配置:
{
"authHost": "https://example.com/api","authEndpoint": "/broadcasting/auth","clients": [
{
"appId": "xxx","key": "xxxxxxx"
}
],"database": "redis","databaseConfig": {
"redis": {},"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},"devMode": true,"host": null,"port": "6001","protocol": "http","socketio": {},"secureOptions": 67108864,"sslCertPath": "","sslKeyPath": "","sslCertChainPath": "","sslPassphrase": "","subscribers": {
"http": true,"redis": true
},"apiOriginAllow": {
"allowCors": true,"allowOrigin": "*","allowMethods": "GET,POST","allowHeaders": "Origin,Content-Type,X-Auth-Token,X-Requested-With,Accept,Authorization,X-CSRF-TOKEN,X-Socket-Id"
}
}
解决方法
由于它在本地运行但不在生产环境中运行,因此您需要检查域上的 socket.io 路径是否已代理到 Laravel 回显服务器。要检查此路径,请在浏览器中运行此路径,它必须返回一个数组。如果它没有被代理,你将根据 Laravel 规则重定向。
https://example.com/socket.io must return:
{"code":0,"message":"Transport unknown"}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。