我一直在开发这个 NodeJS 应用程序,它利用 Laravel Echo 的功能通过套接字连接从服务器接收信息。
带有 Laravel 5.7.19 的服务器端Laravel Echo Server
客户端“laravel-echo”:“^1.5.2”“socket.io”:“^2.2.0”
import Echo from '../../node_modules/laravel-echo/dist/echo.common.js'
import Socketio from 'socket.io-client';
let echo = new Echo({
broadcaster: 'socket.io',
host: 'https://smartfish.danymota.com:8080/',
encrypted: true,
secure: true,
client: Socketio,
auth: {
headers: {
'Authorization': 'Bearer ' + this.token.bearerToken,
},
},
});
echo.private('central.' + macAddress)
.listen('RulesUpdated', (response) => {
handleRules(JSON.parse(response.aquarios))
console.log(new Date().toLocaleString() + " - Rules updated")
})
问题在 Http 中一切正常,当我切换到 HTTPS 时它就停止工作了。此外,套接字连接没有到达服务器(或者至少 Laravel-echo-server 没有记录它)
重要 - 我试过的
通过 Browserify 运行应用程序,然后在浏览器上运行(它在浏览器上运行得很好,即使使用 HTTPS)
使用不同的端口(同样,它适用于 HTTP,因此端口可能不是问题)
将 URL 更改为 wss://, /socket.io
强制 socket.io 在选项上包含一个 secure: true
更改了 Laravel Echo 的版本
尝试同时导入 echo.common.js 和 echo.js
笔记
/api/broadcasting/auth - 这是有效的,所以问题可能不在这里
Laravel 回显服务器配置
{
"authHost": "https://smartfish.danymota.com",
"authEndpoint": "/api/broadcasting/auth",
"clients": [{
"appId": "f7506b5e7118092c",
"key": "9015d93999f3a2f7f95a054a76fbcbfd"
}],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath1": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "8080",
"protocol": "https",
"socketio": {},
"sslCertPath": "/home/danymota/ssl/cert/smartfish.danymota.com.crt",
"sslKeyPath": "/home/danymota/ssl/private/smartfish.danymota.com.key",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "http://smartfishweb.test/api",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
Socket.io 调试
socket.io-client:url parse https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client new io instance for https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client:manager readyState closed +0ms
socket.io-client:manager opening https://smartfish.danymota.com:8080/socket.io +0ms
socket.io-client:manager connect attempt will timeout after 20000 +4ms
socket.io-client:manager readyState opening +1ms
socket.io-client:manager connect_error +60ms
socket.io-client:manager cleanup +0ms
谢谢大家。
我通过向rejectUnauthorized: false
laravel echo添加一个标志解决了这个问题。
this.echo = new Echo({
broadcaster: 'socket.io',
host: config.ECHO_SERVER,
client: Socketio,
rejectUnauthorized: false,
auth: {
headers: {
'Authorization': 'Bearer ' + this.token.bearerToken,
},
},
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句