Laravel-echo接收事件但不加入频道监听
Laravel-echo receives events but doesn't join channel listeners
首先,我有一个实时聊天应用程序(Vue 作为前端,Laravel 作为后端)
在本地主机上它完美运行,但在生产环境中运行不正常。
我注意到我确实收到了活动,但我一直无法加入任何频道。
以上就是所有的信息了,希望大家有什么解决办法。谢谢!
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-回显配置:
{
"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 回显服务器。要在浏览器中检查此路径 运行 此路径,它必须 return 一个数组。如果没有代理,您将根据 laravel 规则重定向。
https://example.com/socket.io must return:
{"code":0,"message":"Transport unknown"}
首先,我有一个实时聊天应用程序(Vue 作为前端,Laravel 作为后端)
在本地主机上它完美运行,但在生产环境中运行不正常。
我注意到我确实收到了活动,但我一直无法加入任何频道。
以上就是所有的信息了,希望大家有什么解决办法。谢谢!
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-回显配置:
{
"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 回显服务器。要在浏览器中检查此路径 运行 此路径,它必须 return 一个数组。如果没有代理,您将根据 laravel 规则重定向。
https://example.com/socket.io must return:
{"code":0,"message":"Transport unknown"}