[英]Paho MQTT: Possible importing error?
I recently downloaded paho-mqtt
via yarn. 我最近通过纱线下载了
paho-mqtt
。 The problem is I am not sure if I am importing it correctly, because I am getting an error: 问题是我不确定我是否正确导入它,因为我收到一个错误:
Cannot read property 'Client' of undefined
无法读取未定义的属性“客户端”
The way I am importing and using it is like this: 我导入和使用它的方式是这样的:
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 = a string for the IP location.host = IP的字符串
location.port = a number for the port location.port =端口号
location.clientID = a string for the clientID location.clientID = clientID的字符串
If it is relevant, I am attempting to use it within a React Native app. 如果它是相关的,我试图在React Native应用程序中使用它。
Maybe this module is not meant to be downloaded via NPM or Yarn? 也许这个模块不是要通过NPM或纱线下载? Or maybe I am not supposed to be importing "Paho"?
或者也许我不应该导入“Paho”?
EDIT: when using react-native-paho-mqtt
--this is the code I am using: 编辑:当使用
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
}
I notice that whenever I enter anything that isn't prefaced with ws:// (web sockets) I would get a URI error. 我注意到,每当我输入任何不以ws://(web套接字)为前缀的内容时,我都会收到URI错误。
The paho-mqtt library has changed, and the example code is incorrect paho-mqtt库已更改,示例代码不正确
var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)
Should be changed to (remove MQTT from Object path): 应更改为(从对象路径中删除MQTT):
var client = new Paho.Client(location.host, location.port, location.clientID)
See the "breaking changes" in the GitHub README page: paho.mqtt.javascript 请参阅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.