Laravel 使用 NodeJS HTTPS 的 Echo 不工作
Laravel Echo with NodeJS HTTPS doesn't work
我一直在开发这个 NodeJS 应用程序,它利用 Laravel Echo 的功能通过套接字连接从服务器接收信息。
服务器端
Laravel 带有 Laravel 5.7.19
的 Echo 服务器
客户端
"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 包含一个安全选项:true on options
更改了 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
提前谢谢大家。
那是因为您必须设置 laravel 回显服务器设置
使用此命令laravel-echo-server init
并在设置协议时选择https
或打开laravel-echo-server.json
并将协议更改为 https
{
"authHost": "https://smartfish.danymota.com:8080",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "It generates it from the command init",
"key": "It generates it from the command init"
}
],
"database": "Your database driver",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001", // your node js port the default is 6001
"protocol": "https", // change it here
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "Your domain with the port",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
我通过向 laravel echo 添加标志 rejectUnauthorized: false
解决了这个问题。
this.echo = new Echo({
broadcaster: 'socket.io',
host: config.ECHO_SERVER,
client: Socketio,
rejectUnauthorized: false,
auth: {
headers: {
'Authorization': 'Bearer ' + this.token.bearerToken,
},
},
})
我一直在开发这个 NodeJS 应用程序,它利用 Laravel Echo 的功能通过套接字连接从服务器接收信息。
服务器端 Laravel 带有 Laravel 5.7.19
的 Echo 服务器客户端 "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 包含一个安全选项:true on options
更改了 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
提前谢谢大家。
那是因为您必须设置 laravel 回显服务器设置
使用此命令laravel-echo-server init
并在设置协议时选择https
或打开laravel-echo-server.json
并将协议更改为 https
{
"authHost": "https://smartfish.danymota.com:8080",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "It generates it from the command init",
"key": "It generates it from the command init"
}
],
"database": "Your database driver",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001", // your node js port the default is 6001
"protocol": "https", // change it here
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": true,
"allowOrigin": "Your domain with the port",
"allowMethods": "GET, POST",
"allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
}
}
我通过向 laravel echo 添加标志 rejectUnauthorized: false
解决了这个问题。
this.echo = new Echo({
broadcaster: 'socket.io',
host: config.ECHO_SERVER,
client: Socketio,
rejectUnauthorized: false,
auth: {
headers: {
'Authorization': 'Bearer ' + this.token.bearerToken,
},
},
})