Laravel Websockets 错误:(WebSocket 在连接建立之前已关闭)

3
我正在尝试在Laravel中使用包创建一个RPC WebSocket。
当我点击连接按钮时,我能够在laravel-websockets端点看到仪表板,但它一直在控制台显示警告。
WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established.

TestSockerHandler.php:

<?php

namespace App\Websocket\SocketHandler;


use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;

class TestSocketHandler implements MessageComponentInterface
{

    function onOpen(ConnectionInterface $conn)
    {
        // TODO: Implement onOpen() method.
        dd($conn->httpRequest);
    }

    function onClose(ConnectionInterface $conn)
    {
        // TODO: Implement onClose() method.
    }

    function onError(ConnectionInterface $conn, \Exception $e)
    {
        // TODO: Implement onError() method.
    }

    public function onMessage(ConnectionInterface $conn, MessageInterface $msg)
    {
        // TODO: Implement onMessage() method.
    }
}

web.php:

\BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter::webSocket('/socket/test', \App\Websocket\SocketHandler\TestSocketHandler::class);

.env

BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

PUSHER_APP_ID=livepost
PUSHER_APP_KEY=livepost_key
PUSHER_APP_SECRET=livepost_secret
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT="/etc/letsencrypt/live/mydomain.com/fullchain.pem"
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK="/etc/letsencrypt/live/mydomain.com/privkey.pem"
LARAVEL_WEBSOCKETS_SSL_PASSPHRASE=""
LARAVEL_WEBSOCKETS_PORT=6001

composer.json:

"pusher/pusher-php-server": "7.0.2"
"beyondcode/laravel-websockets": "^1.14"

我正在使用Ubuntu服务器,并定义了用于WebSocket命令的Supervisor。

/etc/supervisor/conf.d/websockets.conf:

[program:websockets]
process_name=%(program_name)s
command=sudo php artisan websockets:serve --port=8443
directory=/home/my-app/www
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/home/my-app/www/storage/logs/websockets.log

supervisor 状态:

root@my-app:/etc/supervisor/conf.d# supervisorctl status
websockets                       RUNNING   pid 872475, uptime 1:41:26

我已经浪费了两天时间但是没找到原因。只是因为没有任何错误,我不知道检查或搜索什么。有没有任何保存错误日志之类的东西?我一直在持续检查 laravel.log 和 nginx 的 error.log,但是关于 WebSocket 没有任何记录。

1个回答

0

对于可能遇到同样问题的人,我只需要在.env文件和config/websockets.php中将端口从6001更改为8443即可。


你为什么把端口从6001改成了8443?这是你的Websocket正在使用的新端口吗? - Macdonald
你为什么把端口6001改成了8443?这是你的WebSocket运行的新端口吗? - Macdonald
@Macdonald 因为我已经在 /etc/supervisor/conf.d/websockets.conf 中定义了 command=sudo php artisan websockets:serve --port=8443 - V&#252;sal H&#252;seynli
@Macdonald 因为我在 /etc/supervisor/conf.d/websockets.conf 中定义了 command=sudo php artisan websockets:serve --port=8443 - Vüsal Hüseynli
哦,好的。我不认为这对我有用,因为我正在使用安装了Laravel Websocket的Docker容器。该容器已经具有sudo权限,但WebSocket仍无法正常工作。 - Macdonald
哦,好的。我不认为这对我有用,我正在使用一个安装了Laravel websocket的Docker容器。该容器已经具有sudo权限,但是websocket仍然无法工作。 - Macdonald

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接