我已经在客户端组合了两个字符串以发布主题
client.publish(topic,payload = String(message)+","+String(weather))
我在服务器端以字符串形式接收它们 但这是我必须在两个不同的http地址发送的两个不同的消息 我想在服务器端拆分消息 但是split函数不起作用
这是错误消息。
TypeError:无法读取未定义的属性“ split”
//MQTT publisher
var mqtt = require('mqtt')
var fetch = require('node-fetch')
var client = mqtt.connect('mqtt://localhost:1884')
var topic = 'local/temperature'
//var message = 'The status of weather station'
client.on('connect',()=>{
setInterval(async function(req,res){
let weatherResponse = await fetch("https://api.openweathermap.org/data/2.5/weather?q=villingen-schwenningen,de&units=metric&appid=4b66b441cf82f4bca0467ecebb363a79");
let weatherObj = await weatherResponse.json()
let temperature = weatherObj.main.temp
let message = JSON.stringify(temperature)
let weather = JSON.stringify(weatherObj)
client.publish(topic,"+String(weather))
console.log(payload)
console.log('Temperature value sent!')
console.log('The temperature is',message)
},3000)
})
//MQTT server (mosca broker)
const mosca = require("mosca")
var temperature = 0;
moscaSettings = {
host: "localhost",port: 1884,persistence: { factory: mosca.persistence.Memory }
}
var server = new mosca.Server(moscaSettings);
server.on('clientConnected',function(client) {
console.log('client connected',client.id);
});
server.on('published',function(packet,client) {
console.log('Published',packet.payload.toString());
// I want to split the payload here
//console.log(info);
});
server.on('ready',setup);
function setup() {
console.log('Mosca server is up and running on port '+ moscaSettings.port);
}