0
我正在使用Eclipse Paho - Mqtt JavaScript庫。Eclipse Paho - MQTT Javascript - 如何從外部訪問onMessageArrived
試圖從外部函數訪問onMessageArrived
:
mqttConnect(){
var client = new Paho.MQTT.Client("wss://test.mqtt.address", "myClientId");
this.connectionInfo = client;
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
client.subscribe("some/path/to/subscribe/");
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
function onMessageArrived(message) {
console.log("onMessageArrived: "+message.payloadString);
return message.payloadString; //this does not work of course.
}
}
試圖從外面像訪問郵件:
_constract(){
var message = mqttConnect();
console.log(message);
}
也許註冊一些全局變量,並把它放在那裏像this.global = message.payloadString
?
謝謝您的回答。你能舉個例子嗎? –
@ mr_e30添加了示例 – hardillb