Laravel Echo与NodeJS HTTPS不起作用

时间:2019-01-08 11:43:41

标签: node.js laravel sockets socket.io laravel-echo

我一直在开发此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没有记录它)

重要提示-我尝试过的事情

  1. 通过Browserify运行应用程序,然后在浏览器上运行(即使在使用HTTPS的情况下,它也可以在浏览器上正常运行

  2. 在不同的端口上玩(同样,它与HTTP一起使用,因此端口可能不是问题)

  3. 将URL更改为wss://、/socket.io

  4. 强制socket.io包含安全性:选项为true

  5. 更改了Laravel Echo的版本

  6. 试图同时导入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

谢谢大家。

2 个答案:

答案 0 :(得分:0)

因为必须设置laravel echo服务器设置

使用此命令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"
}
}

答案 1 :(得分:0)

我通过在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,
    },
  },
})