Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions phpMQTT.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ public function ping(): void
$this->_debugMessage('ping sent');
}

/**
* Sends a keep qos 1+ PUBACK
*/
public function puback($mid): void
{
$head = chr(0x04);
$head .= chr(0x02);
fwrite($this->socket, $head, 2);
$this->timesinceping = time();
$buffer = chr($mid >> 8);
$buffer .= chr($mid % 256);
fwrite($this->socket, $buffer, 2);
$this->_debugMessage('PUBACK '.$mid);
}

/**
* sends a proper disconnect cmd
*/
Expand Down Expand Up @@ -416,7 +431,8 @@ public function publish($topic, $content, $qos = 0, $retain = false): void

$head{0} = chr($cmd);
$head .= $this->setmsglength($i);


$this->_debugMessage('msg id:'.$id.' = '. $content);
fwrite($this->socket, $head, strlen($head));
$this->_fwrite($buffer);
}
Expand Down Expand Up @@ -453,6 +469,7 @@ public function message($msg)
$topic = substr($msg, 2, $tlen);
$msg = substr($msg, ($tlen + 2));
$found = false;
$msg_id=0;
foreach ($this->topics as $key => $top) {
if (preg_match(
'/^' . str_replace(
Expand All @@ -474,22 +491,33 @@ public function message($msg)
) . '$/',
$topic
)) {
if ($top['qos'] > 0) {
$msg_id = (ord($msg{0}) << 8) + ord($msg{1});
$msg = substr($msg, 2, $tlen-2);
}
if ($top['function'] === '__direct_return_message__') {
if($msg_id){
$this->puback($msg_id);
$this->_debugMessage("msg id:$msg_id PubBack");
}
return $msg;
}

if (is_callable($top['function'])) {
call_user_func($top['function'], $topic, $msg);
} else {
$this->_errorMessage('Message received on topic ' . $topic. ' but function is not callable.');
}
$found=true;
}
}

if ($found === false) {
$this->_debugMessage('msg received but no match in subscriptions');
}

if($msg_id){
$this->puback($msg_id);
$this->_debugMessage("msg id:$msg_id PubBack");
}
return $found;
}

Expand Down