5-GPS室外定位导航车的web mqtt完成通信
这里给出的方案是使用paho_mqtt.js 和mqttws31.js 实现。
html中调用标准库 api 接口实现 。通信使用
cJson 格式 键值对 。
例如
lonti:112.346233
lati:40.34555
function MQTTconnect(){ // console.log(devid); client = new Paho.MQTT.Client(serverip, Number(port), "clientId1"); client.onConnectionLost = onConnectionLost;//注册连接断开处理事件 client.onMessageArrived = onMessageArrived;//注册消息接收处理事件 client.connect({onSuccess:onConnect});//连接服务器并注册连接成功处理事件 }; function onConnect() { // Once a connection has been made, make a subscription and send a message. console.log("onConnect"); client.subscribe(topic); // message = new Paho.MQTT.Message("Hello"); // message.destinationName = "/World"; // client.send(message); }; function onConnectionLost(responseObject) { if (responseObject.errorCode !== 0) console.log("onConnectionLost:"+responseObject.errorMessage); console.log("连接已断开"); }; function onMessageArrived(message) { //console.log("收到消息:"+message.payloadString); // console.log("主题:"+message.destinationName); // console.log("长度:"+strlen(message.payloadString)); if(strlen(message.payloadString)>30) { // heading.update(headingI);//0~360 00 var temp1 = jQuery.parseJSON(message.payloadString); //console.log("heading"+temp1.heading); console.log("解析出来的:humidity:"+temp1.roll); console.log("解析出来的:temperature:"+temp1.pitch); headingI =temp1.heading; roll = temp1.roll; pitch =temp1.pitch; cpuload =temp1.cpuload; cputemp = temp1.cputemp; wifisignal =temp1.wifisignal; $('#heading').val(headingI); $('#wifistrength').val(wifisignal); $('#cputem').val(cputemp); $('#cpuload').val(cpuload); }