-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
acrazing
committed
Aug 31, 2015
1 parent
909778e
commit 5cda709
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|