Beispiel #1
0
<?php

require __DIR__ . '/../test.inc.php';
ini_set('error_log', __DIR__ . '/../../logs/examples_publish.log');
use sskaje\mqtt\MQTT;
use sskaje\mqtt\Debug;
$mqtt = new MQTT($MQTT_SERVER);
# Set Socket Context
$context = stream_context_create();
$mqtt->setSocketContext($context);
# Set Connect Will
$mqtt->setWill('sskaje/will', 'Ciao~', 0, 0);
Debug::Enable();
Debug::SetLogPriority(Debug::NOTICE);
#
$mqtt->setKeepalive(30);
//$mqtt->setAuth('sskaje', '123123');
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
$msg = str_repeat('1234567890', 1);
# Set Retry Timeout for Publish and its following commands
$mqtt->setRetryTimeout(5);
Debug::Log(Debug::INFO, "QoS=1");
$c = 1;
while (true) {
    # Special thanks to @LiuYongShuai for this test case.
    # It is the responsibility of the Client to ensure that the interval between Control Packets
    # being sent does not exceed the Keep Alive value. In the absence of sending any other Control
    # Packets, the Client MUST send a PINGREQ Packet [MQTT-3.1.2-23].
Beispiel #2
0
<?php

require __DIR__ . '/../test.inc.php';
use sskaje\mqtt\MQTT;
use sskaje\mqtt\Debug;
use sskaje\mqtt\MessageHandler;
$client_id = isset($argv[1]) ? $argv[1] : time();
$mqtt = new MQTT($MQTT_SERVER, $client_id);
$context = stream_context_create();
$mqtt->setSocketContext($context);
$mqtt->setVersion(MQTT::VERSION_3_1_1);
#Debug::Enable();
# $mqtt->setAuth('sskaje', '123123');
$mqtt->setKeepalive(3600);
# Set Connect Will
$mqtt->setWill('sskaje/bot/logout', $client_id, 1, 0);
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
$topics['sskaje/bot/broadcast'] = 2;
$topics['sskaje/bot/' . $client_id] = 2;
$mqtt->subscribe($topics);
class BotAgentCallback extends MessageHandler
{
    public function publish(MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $publish_object)
    {
        printf("I got a message:(msgid=%d, QoS=%d, dup=%d, topic=%s) %s\n", $publish_object->getMsgID(), $publish_object->getQoS(), $publish_object->getDup(), $publish_object->getTopic(), $publish_object->getMessage());
    }
    public function suback(MQTT $mqtt, \sskaje\mqtt\Message\SUBACK $suback_object)
    {
Beispiel #3
0
<?php

require __DIR__ . '/../test.inc.php';
use sskaje\mqtt\MQTT;
use sskaje\mqtt\Debug;
use sskaje\mqtt\MessageHandler;
$mqtt = new MQTT($MQTT_SERVER);
$context = stream_context_create();
$mqtt->setSocketContext($context);
# Debug::Enable();
$mqtt->setVersion(MQTT::VERSION_3_1_1);
# $mqtt->setAuth('sskaje', '123123');
$mqtt->setKeepalive(3600);
# Set Connect Will
$mqtt->setWill('sskaje/bot/broadcast', "I'll be back", 1, 0);
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
$topics['sskaje/bot/#'] = 2;
$mqtt->subscribe($topics);
class BotControlCallback extends MessageHandler
{
    protected $online_agents = array();
    public function publish($mqtt, \sskaje\mqtt\Message\PUBLISH $publish_object)
    {
        $topic = $publish_object->getTopic();
        $message = $publish_object->getMessage();
        if ($topic === 'sskaje/bot/login') {
            $this->online_agents[$message] = 1;
            $mqtt->publish_async('sskaje/bot/' . $message, 'Welcome, ' . $message, 1);