我試圖做一個PHP
MQTT客戶端和我的經紀人之間的連接,但我得到這個錯誤:Mosquitto-PHP - 與經紀人TLS錯誤連接
Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'A TLS error occurred.' in /path/testemqtt.php:27 Stack trace: #0 /path/testemqtt.php(27): Mosquitto\Client->connect('localhost', 9419) #1 {main} thrown in /path/testemqtt.php on line 27
我已經做了像Java
,Android
和Javascript (w/ node.js)
,但在PHP
我現在面臨一些困難另一語言相同的連接...這裏是不起作用的代碼:
ini_set('display_errors',1);error_reporting(E_ALL);
/* Construct a new client instance, passing a client ID of 「MyClient」 */
$client = new Mosquitto\Client('PHPClient',true);
/* Set the callback fired when the connection is complete */
$client->onConnect(function($code, $message) use ($client) {
/* Subscribe to the broker's $SYS namespace, which shows debugging info */
echo $code .' - '.$message;
$client->subscribe('pedro/php', 1);
});
/* Set the callback fired when we receive a message */
$client->onMessage(function($message) {
/* Display the message's topic and payload */
echo $message->topic, "\n", $message->payload, "\n\n";
});
/* Connect, supplying the host and port. */
$client->setCredentials('username', 'password');
$client->setTlsCertificates('ca.crt', 'ca_client.crt', 'client.key', null); //As my TLS/SSL certificate is one way I dont need to use passphrase to connect to the broker
$client->setTlsOptions(Mosquitto\Client::SSL_VERIFY_PEER,"tlsv1",null);
$client->connect('localhost', 8883);
/* Enter the event loop */
$client->loopForever();
這裏是實施的例子(它就像一個魅力):
var KEY = fs.readFileSync('client.key');
var CERT = fs.readFileSync('client.crt');
var CAfile = fs.readFileSync('ca.crt');
var MQTToptions = {
host: 'localhost',
clientId: 'pedroNodeJS',
username: 'username',
password: 'password',
port: 8883,
ca: CAfile,
keyPath: KEY,
certPath: CERT,
secureProtocol: 'TLSv1_method',
protocol: 'mqtts',
protocolId: 'MQIsdp',
protocolVersion: 3,
rejectUnauthorized: false,
connectTimeout: 2000,
keepalive:0,
reschedulePings: false
};
var client = mqtt.connect(MQTToptions);
我不知道是什麼問題,因爲顯然PHP
代碼是正確的。
我在我的實現中使用此引用:
MQTT Client Library Encyclopedia – Mosquitto-PHP
感謝所有幫助!