Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better message publish code #111

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 14 additions & 18 deletions core/class/jMQTT.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,42 +1188,38 @@ public function publishMosquitto($id, $eqName, $topic, $payload, $qos, $retain)
// is not executed on the same thread as the deamon. So we do create a new client.
$client = $this->getBroker()->getMosquittoClient($mosqId);

$messageId = 0;

$client->onConnect(
function () use ($client, $topic, $payload, $qos, $retain) {
function () use ($client, $topic, $payload, $qos, $retain, &$messageId) {
$this->log('debug',
'Publication du message ' . $topic . ' ' . $payload . ' (pid=' . getmypid() . ', qos=' . $qos .
', retain=' . $retain . ')');
$client->publish($topic, $payload, $qos, (($retain) ? true : false));

// exitLoop instead of disconnect:
// . otherwise disconnect too early for Qos=2 see below (issue #25)
// . to correct issue #30 (action commands not run immediately on scenarios)
$client->exitLoop();
$messageId = $client->publish($topic, $payload, $qos, (($retain) ? true : false));
});

$client->onPublish(
function ($publishedId) use ($client, &$messageId) {
if($publishedId == $messageId) {
$client->disconnect();
}
});

// Connect to the broker
$client->connect($mosqHost, $mosqPort, 60);

// Loop around to permit the library to do its work
// This function will call the callback defined in `onConnect()` and exit properly
// when the message is sent and the broker disconnected.
// This function will call the callback defined in `onConnect()` and publish the message
// once the message is published, `onPublish()` is called to disconnect properly the client
// then this loopForever ends by itself once disconnected
$client->loopForever();

// For Qos=2, it is nessary to loop around more to permit the library to do its work (see issue #25)
if ($qos == 2) {
for ($i = 0; $i < 30; $i ++) {
$client->loop(1);
}
}

$d = date('Y-m-d H:i:s');
$this->setStatus(array('lastCommunication' => $d, 'timeout' => 0));
if ($this->getType() == self::TYP_EQPT) {
$this->getBroker()->setStatus(array('lastCommunication' => $d, 'timeout' => 0));
}

$client->disconnect();

$this->log('debug', 'Message publié');
}

Expand Down