Laravel 在 HTTPS 本地服务器上的 WebSocket

huangapple go评论63阅读模式
英文:

Laravel websocket on https local server

问题

以下是翻译好的部分:

我正在尝试实现一个本地网络可访问的Laravel应用程序,其中包含WebSocket。

在HTTP上一切正常运行,现在我们需要升级为HTTPS以允许Windows桌面通知中的应用程序。

我使用在网络上找到的OpenSSL命令制作了一个自签名证书。

我设法使应用程序在HTTPS上运行,但其WebSocket却无法工作。我按照Laravel WebSocket文档中的每个说明进行了操作,但都没有成功。

服务器设置:
Windows+XAMPP

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'),
            'host' => 'lqs.miso',
            'port' => 6001,
            'scheme' => 'https'
        ],
    ],

bootstrap.js

import Echo from "laravel-echo"

window.Pusher = require('pusher-js');

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

.env

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="C:/xampp/crt/lqs.miso/server.crt"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="C:/xampp/crt/lqs.miso/server.key"

我还在“system32”上将127.0.0.1设置为lqs.miso

我收到连接失败的错误,没有其他错误提示。

英文:

I'm trying to implement a local network-accessible laravel app with websocket.

Everything works well on http, now we need to upgrade to https to allow the app on windows desktop notification.

I made a self-signed certificate using openssl command I found on the net.

I manage to make the app work on https but not its websocket. I did every instructions in the laravel websocket documentation but no to avail.

Server setup:
Windows+XAMPP

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'),
            'host' => 'lqs.miso',
            'port' => 6001,
            'scheme' => 'https'
        ],
    ],

bootstrap.js

import Echo from "laravel-echo"

window.Pusher = require('pusher-js');

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

.env

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="C:/xampp/crt/lqs.miso/server.crt"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="C:/xampp/crt/lqs.miso/server.key"

I also set 127.0.0.1 to lqs.miso on system32

I'm getting connection failed with no other hints of error.

答案1

得分: 1

I was getting the same error on the server when I switched from HTTP to HTTPS. Adding 'verify_peer' => false, to config/websockets.php inside the 'ssl' key fixed it for me.

例如:


'ssl' => [
        /*
         * Path to local certificate file on the 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 may also be contained
         * in a separate file specified by local_pk.
         */
        'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),

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

        /*
         * Passphrase for your local_cert file.
         */
        'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
        'verify_peer' => false,
    ],
英文:

i was getting same error on server when i switched from http to https,
adding 'verify_peer' => false, to config/websockets.php inside 'ssl' key fixed it for me.

for example:


'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' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
        'verify_peer' => false,
    ],

huangapple
  • 本文由 发表于 2023年5月15日 12:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定