对象在函数中不可用

Object not available in function

我有一个应用程序,我在其中订阅了一个关于 MQTT 代理的主题。收到消息后,我需要处理消息中的数据,然后 post 将其返回给同一代理,但主题不同。我正在使用 PHPMQTT 的 Lightning 分支,因为它维护得很好 (github repo)。

我的脚本如下

<?php
require("./vendor/autoload.php");
/**
 * An example callback function that is not inline.
 * @param  \Lightning\Response $response
 */
function callbackFunction($response) {
    $topic = $response->getRoute();
    $wildcard = $response->getWildcard();
    $message = $response->getMessage();
    echo "Message recieved:\n =============\n Topic: $topic\n Wildcard: $wildcard\n Message:\n $message\n\n";
}
$host = "m21.cloudmqtt.com";
$port = 18256;
$clientID = md5(uniqid()); // use a unique client id for each connection
$username = ''; // username is optional
$password = ''; // password is optional
$mqtt = new \Lightning\App($host, $port, $clientID, $username, $password);
// Optional debugging
$mqtt->debug(true);
if (!$mqtt->connect()) {
    echo "Failed to connect\n";
    exit;
}
// Add a new subscription for each topic that is needed
$mqtt->subscribe('net/raw/#', 0, function ($response) {
    $topic = $response->getRoute();
    $message = $response->getMessage();
    $attributes = $response->getAttributes(); // Returns all the attributes received
    $id = $response->attr('id'); // Gets a specific attribute by key. Returns null if not present.
    echo "Message recieved:\n =============\n Topic: $topic\n Attribute - id: $id\n Message:\n $message\n\n";

  $topic_id = 524;
  $message = "0A";
  $mqtt->publish("test/request/yes".$topic_id, $message, 1);
});
// Callback functions can be inline or by name as a string
$mqtt->subscribe('test/request/#', 0, 'callbackFunction');
// Call listen to begin polling for messages
$mqtt->listen();
?>

我可以订阅 'net/raw' 就好了。加工也很好。将其发布回代理时出现问题。如果第 18 行开始的连接对函数不可用,则会出现以下错误:

Notice: Undefined variable: mqtt in C:\wamp64\www\sub.php on line 35

Fatal error: Uncaught Error: Call to a member function publish() on null in C:\wamp64\www\sub.php:35 Stack trace: 0 [internal function]: {closure}(Object(Lightning\Response)) 1 C:\wamp64\www\vendor\brandonhudson\lightning\Lightning\App.php(353): call_user_func(Object(Closure), Object(Lightning\Response)) 2 C:\wamp64\www\vendor\brandonhudson\lightning\Lightning\App.php(424): Lightning\App->message('0A') 3 C:\wamp64\www\sub.php(40): Lightning\App->listen() 4 {main} thrown in C:\wamp64\www\sub.php on line 35

我可以在函数内部创建一个新连接,但我不想在一个应该足够的时候继续打开和关闭新连接。我该怎么做才能使连接在函数内部可用?

您正在使用 closure (anonymous function) 作为方法 response 的回调函数。要将变量继承到闭包中,您可以使用 use 而不是闭包上的其他参数。

所以你可以把response的调用改成下面这样:

// Add a new subscription for each topic that is needed
$mqtt->subscribe('net/raw/#', 0, function ($response) use ($mqtt) {
    $topic = $response->getRoute();
    $message = $response->getMessage();
    $attributes = $response->getAttributes(); // Returns all the attributes received
    $id = $response->attr('id'); // Gets a specific attribute by key. Returns null if not present.
    echo "Message recieved:\n =============\n Topic: $topic\n Attribute - id: $id\n Message:\n $message\n\n";

    $topic_id = 524;
    $message = "0A";
    $mqtt->publish("test/request/yes".$topic_id, $message, 1);
});