-
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.
Merge pull request #1 from mgdm/allow-subclassing-take2
Allow subclassing using the old-school way
- Loading branch information
Showing
2 changed files
with
91 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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()); | ||
|
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