首页 > 解决方案 > 带有 NodeJS HTTPS 的 Laravel Echo 不起作用

问题描述

我一直在开发这个 NodeJS 应用程序,它利用 Laravel Echo 的功能通过套接字连接从服务器接收信息。

带有 Laravel 5.7.19的服务器端 Laravel 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 没有记录它)

重要 - 我尝试过的

  1. 通过 Browserify 运行应用程序,然后在浏览器上运行(它在浏览器上运行良好,即使使用 HTTPS 也是如此

  2. 使用不同的端口(同样,它适用于 HTTP,因此端口可能不是问题)

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

  4. 强制 socket.io 包含一个 secure: 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

谢谢大家。

标签: node.jslaravelsocketssocket.iolaravel-echo

解决方案


那是因为您必须设置 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"
}
}

推荐阅读