首先,我有一个实时聊天应用程序(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-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"
}
}发布于 2021-06-03 13:08:23
由于它在本地工作,而不是在生产中,因此您需要检查域上的socket.io路径是否被代理到laravel服务器。若要检查此路径,请在浏览器中运行此路径,并且必须返回一个数组。如果没有代理的话,你就会按照拉里威尔的规则被重定向。
https://example.com/socket.io must return:
{"code":0,"message":"Transport unknown"}https://stackoverflow.com/questions/67676458
复制相似问题