5

我正在尝试构建一个聊天应用程序,并为此使用beyondcode/laravel-websockets。当我在本地使用它时,它运行良好,但在服务器上无法使其运行。

我的配置如下

我的回声设置 (Js/Bootstrap.js)

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: false,
    wsHost: window.location.hostname,
    wsPort: 6001,
});

我的推送器设置 (Broadcasting.php)

'pusher' => [
     'driver' => 'pusher',
     'key' => env('PUSHER_APP_KEY'),
     'secret' => env('PUSHER_APP_SECRET'),
     'app_id' => env('PUSHER_APP_ID'),
  'options' => [
     'cluster' => env('PUSHER_APP_CLUSTER'),
     'encrypted' => false,
     'host' => env('WEBSOCKET_HOST'),
     'port' => env('WEBSOCKET_PORT'),
     'scheme' => env('WEBSSOCKET_SCHEME'),
 ],
],

我的 Webscoket 设置 (Websockets.php)

'dashboard' => [
       'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
],

 'apps' => [
        [
            'id' => env('PUSHER_APP_ID'),
            'name' => env('APP_NAME'),
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'path' => env('PUSHER_APP_PATH'),
            'capacity' => null,
            'enable_client_messages' => true,
            'enable_statistics' => true,
        ],
    ],

我的 .env 配置

PUSHER_APP_ID=MyDefaultIdForURMENU
PUSHER_APP_KEY=1234567
PUSHER_APP_SECRET=MyDefaultSecretForURMENU
PUSHER_APP_CLUSTER=mt1

WEBSOCKET_HOST = 127.0.0.1

WEBSOCKET_PORT = 6001

WEBSSOCKET_SCHEME= http

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

现在,当我直接在 ssh 中运行 websocket 或使用命令使用 setsid 时

php artisan websockets:serve

或者

setsid php artisan websockets:serve

表明

在端口 6001 上启动 WebSocket 服务器...

但控制台日志显示

WebSocket 连接到“wss://mydomain.com/app/1234567?protocol=7&client=js&version=5.0.3&flash=false”失败:WebSocket 握手期间出错:意外响应代码:404

或 有时

WebSocket 连接到 'ws://mydomain.com:6001/app/1234567?protocol=7&client=js&version=5.0.3&flash=false' 失败:WebSocket 在连接建立之前关闭。

请指导我我做错了什么

4

1 回答 1

1

大家好,我遇到了这个问题,我从上到下搜索了互联网,最后做了以下操作以使其在 VPS 上运行

1-配置/广播.php

'pusher' => [
    'driver' => 'pusher',
    'key' => env('PUSHER_APP_KEY'),
    'secret' => env('PUSHER_APP_SECRET'),
    'app_id' => env('PUSHER_APP_ID'),
    'options' => [
        'useTLS' => true,
        'encrypted' => true,
        'host' => '127.0.0.1',
        'port' => 6001,
        'scheme' => 'https',
        'curl_options' => [
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ]
    ]
]

2-资源/js/bootstrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    forceTLS: true,
    encrypted: true,
    wsHost: window.location.hostname,
    wsPort: 6001,
    wssPort: 6001,
    enabledTransports: ['ws','wss'],
    disableStats: true
});

3-配置/websockets.php

'ssl' => [
    /*
     * Path to local certificate file on filesystem. It must be a PEM encoded file which
     * contains your certificate and private key. It can optionally contain the
     * certificate chain of issuers. The private key also may be contained
     * in a separate file specified by local_pk.
     */
    'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),

    /*
     * Path to local private key file on filesystem in case of separate files for
     * certificate (local_cert) and private key.
     */
    'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),

    /*
     * Passphrase for your local_cert file.
     */
    'passphrase' => null,
    'verify_peer' => false
],

4- .env

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="../ssl/certs/filename.crt"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="../ssl/keys/filename.key"

第 3 步和第 4 步也很重要,就像第 1 步和第 2 步一样,您需要提供 .crt 和 .key 文件路径。对于不同的服务器,路径可能不同,您可以向上移动到 cpanel 文件管理器中的 public_html 文件夹并找到这些文件的 ssl 文件夹。

于 2021-10-08T04:02:56.167 回答