diff --git a/README.md b/README.md index a8a62b5..531adc1 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ This is the actual Mosquitto client. 1. [onLog](#onlog) - set the logging callback 1. [onSubscribe](#onsubscribe) - set the subscribe callback 1. [onMessage](#onmessage) - set the callback fired when a message is received +1. [onPublish](#onPublish) - set the callback fired when a message is published 1. [setMaxInFlightMessages](#setmaxinflightmessages) - set the number of QoS 1 and 2 messages that can be "in flight" at once 1. [setMessageRetry](#setmessageretry) - set the number of seconds to wait @@ -296,6 +297,24 @@ The callback should take parameters of the form: | message | Mosquitto\Message | A Message object containing the message data | +#### onPublish + +Set the publish callback, This is called when a message is publish by the client itself. + +**Warning**: this may be called before the method `Mosquitto\Client::publish` return the +message id, so, you need to create a queue to deal with the mid list + +| Parameter | Type | Description | +| --- | --- | --- | +| callback | callback | The callback | + +The callback should take parameters of the form: + +| Parameter | Type | Description | +| --- | --- | --- | +| mid | int | the message id returned by `Mosquitto\Client::publish` | + + #### setMaxInFlightMessages Set the number of QoS 1 and 2 messages that can be “in flight” at one time. An @@ -329,6 +348,9 @@ Publish a message on a given topic. | qos | int | Integer value 0, 1 or 2 indicating the QoS for this message | | retain | boolean | If true, make this message retained | +return `int` the message id in broker system +**Warning** the message id is not unique + #### subscribe Subscribe to a topic. diff --git a/examples/.testOnpublish.php.un~ b/examples/.testOnpublish.php.un~ new file mode 100644 index 0000000..ede5470 Binary files /dev/null and b/examples/.testOnpublish.php.un~ differ diff --git a/examples/testOnpublish.php b/examples/testOnpublish.php new file mode 100644 index 0000000..efca373 --- /dev/null +++ b/examples/testOnpublish.php @@ -0,0 +1,65 @@ +id = $mid; + self::$publish[$mid] = $msg; + } + + public static function confirm($mid) { + if(array_key_exists($mid, self::$publish)) { + self::$publish[$mid]->state = true; + } + } + + public static function addReceive($msg) { + $msg = Message::factory($msg, true); + self::$receive[$msg->id] = $msg; + } +} + +class Message { + public $id; + public $state = false; + public $msg; + public static function factory(Mosquitto\Message $msg, $state = false) { + $message = new Message(); + $message->state = $state; + $message->msg = $msg; + $message->id = $msg->mid; + return $message; + } +} +$client = new Mosquitto\Client('client.terminal.onpublish', false); + +$client->onMessage(function($msg) { + print_r(array('receive', $msg)); + MQ::addReceive($msg); +}); + +$client->onPublish(function($mid) { + MQ::confirm($mid); + print_r(array('comfirm publish', MQ::$publish[$mid])); +}); +$client->onConnect(function($rc, $msg) { + print_r(array('rc' => $rc, 'message' => $msg)); +}); + +$client->connect('localhost', 1883, 60); + +sleep(1); + + +$client->subscribe('/test/publish', 1); +$msg = Message::factory(new Mosquitto\Message()); +$msg->msg->topic = '/test/publish'; +$msg->msg->payload = 'hello from on publish'; +$msg->msg->qos = 1; +$mid = $client->publish($msg->msg->topic, $msg->msg->payload, $msg->msg->qos); +print_r(array('publish', $msg)); +MQ::addPublish($mid, $msg); + +sleep(1); + +$client->loopForever(); \ No newline at end of file diff --git a/examples/testOnpublish.php~ b/examples/testOnpublish.php~ new file mode 100644 index 0000000..fedce36 --- /dev/null +++ b/examples/testOnpublish.php~ @@ -0,0 +1,68 @@ +id = $mid; + self::$publish[$mid] = $msg; + } + + public static function confirm($mid) { + if(array_key_exists($mid, self::$publish)) { + self::$publish[$mid]->state = true; + } + } + + public static function addReceive($msg) { + $msg = Message::factory($msg, true); + self::$receive[$msg->id] = $msg; + } +} + +class Message { + public $id; + public $state = false; + public $msg; + public static function factory(Mosquitto\Message $msg, $state = false) { + $message = new Message(); + $message->state = $state; + $message->msg = $msg; + $message->id = $msg->mid; + return $message; + } +} +$client = new Mosquitto\Client('client.terminal.onpublish', false); + +$client->onMessage(function($msg) { + print_r(array('receive', $msg)); + MQ::addReceive($msg); +}); + +$client->onPublish(function($mid) { + MQ::confirm($mid); + print_r(array('comfirm publish', MQ::$publish[$mid])); +}); +$client->onConnect(function($rc, $msg) { + print_r(array('rc' => $rc, 'message' => $msg)); + $client->subscribe('/test/onpublish', 1); + $msg = Message::factory(new Mosquitto\Message()); + $msg->msg->topic = '/test/onpublish'; + $msg->msg->payload = 'hello from on publish'; + $msg->qos = 1; + $mid = $client->publish($msg->msg->topic, $msg->msg->payload, $msg->msg->qos); + MQ::addPublish($mid, $msg); +}); + +$client->connect('localhost', 1883, 60); + +sleep(1); + + +$client->subscribe('/test/onpublish', 1); +$msg = Message::factory(new Mosquitto\Message()); +$msg->msg->topic = '/test/onpublish'; +$msg->msg->payload = 'hello from on publish'; +$msg->qos = 1; +$mid = $client->publish($msg->msg->topic, $msg->msg->payload, $msg->msg->qos); +MQ::addPublish($mid, $msg); +