簡體   English   中英

如何使用Amphp捕獲php websocket斷開的TCP連接異常?

[英]How to catch a php websocket broken TCP connection exception with Amphp?

這是當連接仍然存在時我正在運行的當前 WebSocket 循環。 但是連續連接11小時后,收到異常

"exception":"[object] (Amp\\\\Websocket\\\\ClosedException(code: 1006): The connection was closed: Client closed the underlying TCP connection at ...

如何檢查關閉的連接或異常本身? ,這樣我就可以正確結束腳本邏輯而不會突然失敗。

     \Amp\Loop::run(function () use ($fn, $st)
        {
            $connection = yield \Amp\Websocket\connect('wss://URL');

            yield $connection->send('{"action":"auth","params":"KEYID"}');
            yield $connection->send('{"action":"subscribe","params":"'.$st.'"}');

            $i = 0;

            while ($message = yield $connection->receive()) 
            {
                $i++;
                $payload = yield $message->buffer();

                $r = $fn($payload, $i);

                if ($r == false) {
                    $connection->close();
                    break;
                }
            }
        }
    );

我正在使用這個 Amphp Websocket: https : //github.com/amphp/websocket-client

謝謝!

我確實通過查找ClosedException並在拋出后運行其他任務找到了解決方案。

\Amp\Loop::run(function () use ($fn, $st)
    {
        try 
        {
            $connection = yield \Amp\Websocket\connect('wss://URL');

            yield $connection->send('{"action":"auth","params":"KEYID"}');
            yield $connection->send('{"action":"subscribe","params":"'.$st.'"}');

            $i = 0;

            while ($message = yield $connection->receive()) 
            {
                $i++;
                $payload = yield $message->buffer();

                $r = $fn($payload, $i);

                if ($r == false) {
                    $connection->close();
                    break;
                }
            }
        }
        catch (\Amp\Websocket\ClosedException $e) 
        {
            // do something here
        }
    }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM