コード例 #1
3
ファイル: test.php プロジェクト: sgounane/Mosquitto-PHP
<?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)
{
コード例 #2
1
ファイル: subclass.php プロジェクト: sgounane/Mosquitto-PHP
 public function subscribe($topic, $qos)
 {
     $mid = parent::subscribe($topic, $qos);
     $this->pendingSubs[$mid] = $topic;
 }
コード例 #3
1
ファイル: event.php プロジェクト: sgounane/Mosquitto-PHP
<?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();
コード例 #4
0
ファイル: phpmqtt_input.php プロジェクト: CapeSepias/emoncms
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}");
}
function subscribe()
{
    global $log, $topic;
    echo "Subscribed to topic: " . $topic . "\n";
コード例 #5
0
        $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;
$mid = $client->publish($msg->msg->topic, $msg->msg->payload, $msg->msg->qos);
print_r(array('publish', $msg));
MQ::addPublish($mid, $msg);
sleep(1);
$client->loopForever();
コード例 #6
0
// Use port 8883 for SSL
$_TOPIC = '[domain]/test-stuff/test-thing';
$_PAYLOAD = '{"Hello":"World!"}';
$_QOS = 0;
// Using the PHP Mosquitto extension (https://github.com/mgdm/Mosquitto-PHP).
$client = new Mosquitto\Client();
// Bind callbacks
$client->onConnect('on_connect');
$client->onDisconnect('on_disconnect');
$client->onSubscribe('on_subscribe');
$client->onMessage('on_message');
// Set client credentials.
$client->setCredentials($_USERNAME, $_TOKEN_HASH);
$client->connect($_HOST, $_PORT);
// Subscribe to a topic.
$client->subscribe($_TOPIC, $_QOS);
// Loop forever to maintain a connection with the host.
for (;;) {
    $client->loop();
}
// Define callback functions.
function on_connect($rc, $message)
{
    global $_TOPIC, $_PAYLOAD;
    // Display the connection's result code and explanation message.
    echo 'Code: ' . $rc . ' (' . $message . ')' . PHP_EOL;
    // Publish to a topic.
    $client->publish($_TOPIC, $_PAYLOAD);
}
function on_disconnect()
{