Skip to content

Commit 2c22454

Browse files
authored
Fixed bug that close frame will be lost for websocket-client. (#7318)
1 parent 6b7810a commit 2c22454

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Client.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\WebSocketClient\Exception\ConnectException;
1717
use Psr\Http\Message\UriInterface;
1818
use Swoole\Coroutine;
19+
use Swoole\WebSocket\CloseFrame as SwCloseFrame;
1920
use Swoole\WebSocket\Frame as SwFrame;
2021

2122
class Client
@@ -63,11 +64,12 @@ public function __destruct()
6364
public function recv(float $timeout = -1)
6465
{
6566
$ret = $this->client->recv($timeout);
66-
if ($ret instanceof SwFrame) {
67-
return new Frame($ret);
68-
}
6967

70-
return $ret;
68+
return match (true) {
69+
$ret instanceof SwCloseFrame => new CloseFrame($ret),
70+
$ret instanceof SwFrame => new Frame($ret),
71+
default => $ret,
72+
};
7173
}
7274

7375
/**

src/CloseFrame.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\WebSocketClient;
14+
15+
use Swoole\WebSocket\CloseFrame as SwCloseFrame;
16+
17+
class CloseFrame extends Frame
18+
{
19+
public int $code = WEBSOCKET_CLOSE_NORMAL;
20+
21+
public string $reason = '';
22+
23+
public function __construct(SwCloseFrame $frame)
24+
{
25+
parent::__construct($frame);
26+
27+
$this->code = $frame->code;
28+
$this->reason = $frame->reason;
29+
}
30+
}

0 commit comments

Comments
 (0)