Ejemplo n.º 1
1
<?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->setAuth('sskaje', '123123');
$mqtt->setKeepalive(3600);
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
$topics['sskaje/#'] = 2;
$mqtt->subscribe($topics);
#$mqtt->unsubscribe(array_keys($topics));
class MySubscribeCallback extends MessageHandler
{
    public function publish($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());
    }
}
$callback = new MySubscribeCallback();
$mqtt->setHandler($callback);
$mqtt->loop();
Ejemplo n.º 2
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->setAuth('sskaje', '123123');
$mqtt->setKeepalive(10);
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
$topics['sskaje/#'] = 2;
$mqtt->subscribe($topics);
#$mqtt->unsubscribe(array_keys($topics));
class MySubscribeCallback extends MessageHandler
{
    public function publish($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());
    }
}
$callback = new MySubscribeCallback();
$mqtt->setHandler($callback);
$mqtt->loop();
Ejemplo n.º 3
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].
Ejemplo n.º 4
0
Archivo: test.php Proyecto: sskaje/mqtt
<?php

date_default_timezone_set('Asia/Shanghai');
require __DIR__ . '/test.inc.php';
use sskaje\mqtt\MQTT;
use sskaje\mqtt\Debug;
$mqtt = new MQTT($MQTT_SERVER, '123');
$mqtt->setVersion(MQTT::VERSION_3_1_1);
#$mqtt->setVersion(MQTT::VERSION_3_1);
Debug::Enable();
$mqtt->setKeepalive(60);
$connected = $mqtt->connect();
if (!$connected) {
    die("Not connected\n");
}
log_test('publish() QoS 0');
$mqtt->publish('qos/0', 'This is a QoS 0 Message', 0, 0, 0, 0);
try {
    $mqtt->publish('qos/0', 'This is a QoS 0 Message', 0, 0, 0, 1);
} catch (Exception $e) {
    echo 'Exception: ' . $e->getMessage() . "\n";
}
log_test('publish() QoS 1');
try {
    $mqtt->publish('qos/1', 'This is a QoS 1 Message', 0, 1, 0, 0);
} catch (Exception $e) {
    echo 'Exception: ' . $e->getMessage() . "\n";
}
try {
    $mqtt->publish('qos/1', 'This is a QoS 1 Message', 0, 1, 0, 1);
} catch (Exception $e) {