Skip to content

Commit 7faf34f

Browse files
authored
MQTT Control Packets MUST be sent in WebSocket binary data frames (#110)
1 parent d1a01fb commit 7faf34f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/WebSocketClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
namespace Simps\MQTT;
1414

1515
use Simps\MQTT\Config\ClientConfig;
16+
use Simps\MQTT\Exception\ProtocolException;
1617
use Swoole\Coroutine\Http\Client;
1718
use Swoole\Http\Status;
19+
use Swoole\WebSocket\CloseFrame;
1820
use Swoole\WebSocket\Frame;
1921

2022
class WebSocketClient extends BaseClient
@@ -96,10 +98,16 @@ public function recv()
9698
protected function getResponse()
9799
{
98100
$response = $this->getClient()->recv();
99-
if ($response === false) {
101+
if ($response === false || $response instanceof CloseFrame) {
100102
return false;
101103
}
102104
if ($response instanceof Frame) {
105+
// If any other type of data frame is received the recipient MUST close the Network Connection.
106+
if ($response->opcode !== WEBSOCKET_OPCODE_BINARY) {
107+
$this->getClient()->close();
108+
throw new ProtocolException('MQTT Control Packets MUST be sent in WebSocket binary data frames.');
109+
}
110+
103111
return $response->data;
104112
}
105113

0 commit comments

Comments
 (0)