Skip to content

Commit

Permalink
MQTT Control Packets MUST be sent in WebSocket binary data frames
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Jan 20, 2025
1 parent d1a01fb commit 0c337f9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/WebSocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
namespace Simps\MQTT;

use Simps\MQTT\Config\ClientConfig;
use Simps\MQTT\Exception\ProtocolException;
use Swoole\Coroutine\Http\Client;
use Swoole\Http\Status;
use Swoole\WebSocket\CloseFrame;
use Swoole\WebSocket\Frame;

class WebSocketClient extends BaseClient
Expand Down Expand Up @@ -96,10 +98,16 @@ public function recv()
protected function getResponse()
{
$response = $this->getClient()->recv();
if ($response === false) {
if ($response === false || $response instanceof CloseFrame) {
return false;
}
if ($response instanceof Frame) {
// If any other type of data frame is received the recipient MUST close the Network Connection.
if ($response->opcode !== WEBSOCKET_OPCODE_BINARY) {
$this->getClient()->close();
throw new ProtocolException('MQTT Control Packets MUST be sent in WebSocket binary data frames.');
}

return $response->data;
}

Expand Down

0 comments on commit 0c337f9

Please sign in to comment.