Example #1
0
 public function testUnencryptedAuthorizedClientCreation()
 {
     // Try to establish connection to server
     $client = new LibMQTT\Client("localhost", 1883, "phpUnitClient");
     $client->setAuthDetails("testuser", "userpass");
     $result = $client->connect();
     $this->assertTrue($result);
     // Subscribe to 'libmqtt/test' and 'libmqtt/empty' channels
     $result = $client->subscribe(["libmqtt/test" => ["qos" => 1, "function" => [$this, "handleReceivedMessages"]], "libmqtt/empty" => ["qos" => 0, "function" => [$this, "handleReceivedMessages"]]]);
     $this->assertTrue($result);
     // Publish test message with QoS 0 to channel
     $result = $client->publish("libmqtt/test", "test message 1", 0);
     $this->assertTrue($result);
     // Publish test message with QoS 1 to channel
     $result = $client->publish("libmqtt/test", "test message 2", 1);
     $this->assertTrue($result);
     $this->assertTrue(count($client->getMessageQueue()) == 1);
     // Wait 5 x 20ms for server to send us the previous messages
     for ($l1 = 0; $l1 < 5; $l1++) {
         usleep(20000);
         $client->eventloop();
     }
     // By now, we should have received both messages back to us
     // and also the test message 2 should have been acknowledged
     $this->assertTrue(count($client->getMessageQueue()) == 0);
     $this->assertTrue($this->message1Received);
     $this->assertTrue($this->message2Received);
     //
     $client->close();
 }
Example #2
0
 public function testCryptedClientCreation()
 {
     // Try to establish connection to server
     $client = new LibMQTT\Client("test.mosquitto.org", 8883, "phpUnitClient");
     $client->setCryptoProtocol("tls");
     $client->setCAFile("tests/mosquitto.org.crt");
     //
     $result = $client->connect();
     $this->assertTrue($result);
     // Subscribe to 'libmqtt/test' and 'libmqtt/empty' channels
     $result = $client->subscribe(["libmqtt/test" => ["qos" => 1], "libmqtt/empty" => ["qos" => 0]]);
     $this->assertTrue($result);
     //
     $result = $client->publish("libmqtt/test", "testi", 0);
     $this->assertTrue($result);
     //
     $client->close();
 }
Example #3
0
<?php

// File created by: composer dump-autoload -o
require "../vendor/autoload.php";
// Try to establish connection to server
$client = new LibMQTT\Client("serverName", 8883, "ThisIsYourUniqueClientID");
// Set connection protocol ("tls" = all tls versions)
$client->setCryptoProtocol("tls");
// If server is running with self-signed certificate, provide CA file here
#$client->setCAFile("path_to_cafile");
$result = $client->connect();
// Publish message to "topic1/topic2"
$client->publish("topic1/topic2", "Test Message", 0);
// Close the connection.
$client->close();
Example #4
0
<?php

// File created by: composer dump-autoload -o
require "../vendor/autoload.php";
// Try to establish connection to server
$client = new LibMQTT\Client("serverName", 1883, "ThisIsYourUniqueClientID");
$result = $client->connect();
// Publish message to "topic1/topic2"
$client->publish("topic1/topic2", "Test Message", 0);
// Close the connection.
$client->close();