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

Add onPublish callback #30

Merged
merged 6 commits into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add document
  • Loading branch information
acrazing committed Aug 31, 2015
commit 5cda70908c0b9a483c856cad96ee90b100d312d0
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Binary file added examples/.testOnpublish.php.un~
Binary file not shown.
65 changes: 65 additions & 0 deletions examples/testOnpublish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
class MQ {
public static $publish = array();
public static $receive = array();
public static function addPublish($mid, $msg) {
$msg->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();
68 changes: 68 additions & 0 deletions examples/testOnpublish.php~
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
class MQ {
public static $publish = array();
public static $receive = array();
public static function addPublish($mid, $msg) {
$msg->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);