首页 > 解决方案 > WebSocket 连接到 'wss://***:6001/socket.io/?EIO=3&transport=websocket' 失败:WebSocket 已关闭

问题描述

我正在使用laravel。我想在 SSL 上连接一个套接字连接。

Echo Version: 1.6.2
Laravel Version: 5.7.28
PHP Version: 7.2.31
NPM Version: 6.14.5
Node Version: 11.15.0

我正在使用 SSL 协议。我从前端收到此错误消息:

WebSocket connection to 'wss://example.com:6001/socket.io/?EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established

这是我的 laravel-echo-server.json

{
    "authHost": "https://example.com",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "https",
    "socketio": {},
    "secureOptions": ****,
    "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"
    }
}

还有我的 apache2 配置:

<VirtualHost *.*.*.*:80>
  ServerName example.com
    ServerAlias mail.example.com www.example.com
  DocumentRoot /home/example/public_html
  ServerAdmin webmaster@example.com
  UseCanonicalName Off
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:6001/$1 [P,L]

    ProxyPass        /socket.io/ http://localhost:6001/socket.io
    ProxyPassReverse /socket.io/ http://localhost:6001/socket.io

  ## User example # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir disabled
          UserDir enabled example
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

标签: laravelwebsocket

解决方案


推荐阅读