-
Notifications
You must be signed in to change notification settings - Fork 146
/
subclass.php
53 lines (39 loc) · 1.22 KB
/
subclass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
class MyClient extends Mosquitto\Client {
protected $pendingSubs = [];
protected $grantedSubs = [];
protected $subscribeCallback = null;
public function __construct($id = null, $cleanSession = false) {
parent::__construct($id, $cleanSession);
parent::onSubscribe(array($this, 'subscribeHandler'));
}
public function subscribeHandler($mid, $qosCount, $grantedQos) {
if (!isset($this->pendingSubs[$mid])) {
return;
}
$topic = $this->pendingSubs[$mid];
$this->grantedSubs[$topic] = $grantedQos;
echo "Subscribed to topic {$topic} with message ID {$mid}\n";
if (is_callable($this->subscribeCallback)) {
$this->subscribeCallback($mid, $qosCount, $grantedQos);
}
}
public function subscribe($topic, $qos) {
$mid = parent::subscribe($topic, $qos);
$this->pendingSubs[$mid] = $topic;
}
public function onSubscribe(callable $callable) {
$this->subscribeHandler = $callable;
}
public function getSubscriptions() {
return $this->grantedSubs;
}
}
$c = new MyClient('subscriptionTest');
$c->onSubscribe(function() { echo "Hello, I got subscribed\n"; });
$c->connect('localhost', 1883, 50);
$c->subscribe('#', 1);
for ($i = 0; $i < 5; $i++) {
$c->loop(10);
}
var_dump($c->getSubscriptions());