php 错误:(WebSocket在连接建立前关闭)

xxe27gdn  于 2023-02-21  发布在  PHP
关注(0)|答案(1)|浏览(246)

我试图通过使用Laravel中的beyondcode/laravel-websockets包创建一个RPC WebSocket。
我可以看到laravel-websockets端点上的 Jmeter 板,但当我单击连接按钮时,它一直在控制台中向我显示警告:

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命令定义了管理程序。
/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

我已经浪费了2天的时间,但还是找不到原因,只是因为没有任何错误,我不知道该检查什么,也不知道该搜索什么,是否有错误日志或smth?我一直在检查laravel.log和nginx error.log,但没有任何关于websocket的日志。

7xllpg7q

7xllpg7q1#

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

相关问题