[英]Paho MQTT: Possible importing error?
我最近通過紗線下載了paho-mqtt
。 問題是我不確定我是否正確導入它,因為我收到一個錯誤:
無法讀取未定義的屬性“客戶端”
我導入和使用它的方式是這樣的:
import Paho from 'paho-mqtt';
var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)
const MQTTConnectAndMessage = (message) => {
client.connect({onSuccess: sendMQTTMessage})
}
const sendMQTTMessage = (msg) => {
let message = new Paho.MQTT.Message(msg);
message.destinationName = location.messageDestination;
client.send(message);
}
location.host = IP的字符串
location.port =端口號
location.clientID = clientID的字符串
如果它是相關的,我試圖在React Native應用程序中使用它。
也許這個模塊不是要通過NPM或紗線下載? 或者也許我不應該導入“Paho”?
編輯:當使用react-native-paho-mqtt
這是我正在使用的代碼:
const client = new Client({ uri: 'ws://myiphere/ws', port: 1883, clientId: 'clientId', storage: myStorage});
const sendMQTTMessage = (msg) => {
client.on('connectionLost', (responseObject) => {
if (responseObject.errorCode !== 0) {
console.log("connection lost!");
}
});
client.on('messageReceived', (message) => {
console.log(message.payloadString);
});
client.connect()
.then(() => {
const message = new Message(msg);
message.destinationName = 'F2/BOX2/LED3';
client.send(message);
})
.catch((responseObject) => {
if (responseObject.errorCode !== 0) {
console.log('onConnectionLost:' + responseObject.errorMessage);
}
});
}
export {
sendMQTTMessage
}
我注意到,每當我輸入任何不以ws://(web套接字)為前綴的內容時,我都會收到URI錯誤。
paho-mqtt庫已更改,示例代碼不正確
var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)
應更改為(從對象路徑中刪除MQTT):
var client = new Paho.Client(location.host, location.port, location.clientID)
請參閱GitHub README頁面中的“重大更改”: paho.mqtt.javascript
試試這個react-native兼容庫: https : //www.npmjs.com/package/react-native-paho-mqtt
yarn add react-native-paho-mqtt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.