Exemple #1
3
<?php

$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->onLog('logger');
$client->subscribe('#', 1);
for ($i = 0; $i < 10; $i++) {
    $client->loop();
}
$client->unsubscribe('#');
for ($i = 0; $i < 10; $i++) {
    $client->loop();
}
function connect($r, $message)
{
    echo "I got code {$r} and message {$message}\n";
}
function subscribe()
{
    echo "Subscribed to a topic\n";
}
function unsubscribe()
{
    echo "Unsubscribed from a topic\n";
}
function message($message)
{
function on_disconnect()
{
    global $_HOST, $_PORT, $_USERNAME;
    // Display notice/confirmation of being disconnected.
    echo 'Client disconnected.' . PHP_EOL;
    echo 'Will attempt to reconnect in 5s.' . PHP_EOL;
    sleep(5);
    // Create a new $client object.
    echo 'Trying to reconnect...' . PHP_EOL;
    $client = new Mosquitto\Client();
    $client->connect($_HOST, $_PORT);
    // Bind callbacks
    $client->onConnect('on_connect');
    $client->onDisconnect('on_disconnect');
    $client->onSubscribe('on_subscribe');
    $client->onMessage('on_message');
    for (;;) {
        $client->loop();
    }
}
Exemple #3
1
<?php

$c = new Mosquitto\Client();
$c->onConnect(function ($code, $message) {
    echo "I'm connected\n";
});
$c->connect('localhost', 1883, 60);
$c->subscribe('#', 1);
$c->onMessage(function ($m) {
    var_dump($m);
});
$socket = $c->getSocket();
$base = new EventBase();
$ev = new Event($base, $socket, Event::READ | Event::PERSIST, 'cb', $base);
function cb($fd, $what, $arg)
{
    global $c;
    echo "Triggered\n";
    var_dump(func_get_args());
    $c->loop();
}
$ev->add();
$base->dispatch();
Exemple #4
0
}
require "Modules/user/user_model.php";
$user = new User($mysqli, $redis, null);
require_once "Modules/feed/feed_model.php";
$feed = new Feed($mysqli, $redis, $feed_settings);
require_once "Modules/input/input_model.php";
$input = new Input($mysqli, $redis, $feed);
require_once "Modules/process/process_model.php";
$process = new Process($mysqli, $input, $feed, $user->get_timezone($mqtt_server['userid']));
$mqtt_client = new Mosquitto\Client();
$mqtt_client->setCredentials($mqtt_server['user'], $mqtt_server['password']);
$connected = false;
$mqtt_client->onConnect('connect');
$mqtt_client->onDisconnect('disconnect');
$mqtt_client->onSubscribe('subscribe');
$mqtt_client->onMessage('message');
$mqtt_client->connect($mqtt_server['host'], $mqtt_server['port'], 5);
$topic = $mqtt_server['basetopic'] . "/#";
echo "Subscribing to: " . $topic . "\n";
$log->warn("Subscribing to: " . $topic);
$mqtt_client->subscribe($topic, 2);
while (true) {
    $mqtt_client->loop();
}
function connect($r, $message)
{
    global $connected, $log;
    $connected = true;
    echo "Connected to MQTT server with code {$r} and message {$message}\n";
    global $log;
    $log->warn("Connecting to MQTT server: {$message}: code: {$r}");
{
    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;