0

我是 PHP 世界的新手,在连接 MQTT 时遇到问题。

我正在使用 phpMQTT.php 库,并且正在使用 IP 地址连接到 MQTT 代理。我正在尝试发布到 MQTT 代理,在 phpMQTT.php 库文件中出现错误

错误是: stream_socket_client(): 无法连接到 tcp:// ..* :8083(连接超时)

面临以下代码中的问题:

if ($this->cafile) {
            $socketContext = stream_context_create(["ssl" => [
                "verify_peer_name" => true,
                "cafile" => $this->cafile
                ]]);
            $this->socket = stream_socket_client("tls://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $socketContext);
        } else {
            $this->socket = stream_socket_client("tcp://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT);
        }
4

1 回答 1

0

从 phpMQTT.php 库
(来源https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php),
您必须设置以下详细信息,如源代码中所示。

/* sets the broker details */  

function broker($address, $port, $clientid, $cafile = NULL){
    $this->address = $address;
    $this->port = $port;
    $this->clientid = $clientid;
    $this->cafile = $cafile;
}

如果您正在运行防火墙 - 请同时打开您正在使用的端口。

于 2020-04-12T18:08:37.487 回答