本文共 1048 字,大约阅读时间需要 3 分钟。
mqtt服务器采用activemq实现。
// Create a client instance//注意不要在client前写varclient = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");// set callback handlersclient.onConnectionLost = onConnectionLost;client.onMessageArrived = onMessageArrived;// connect the clientclient.connect({onSuccess:onConnect});// called when the client connectsfunction onConnect() { // Once a connection has been made, make a subscription and send a message. console.log("onConnect"); client.subscribe("World"); message = new Paho.MQTT.Message("Hello"); message.destinationName = "World"; client.send(message);}// called when the client loses its connectionfunction onConnectionLost(responseObject) { if (responseObject.errorCode !== 0) { console.log("onConnectionLost:"+responseObject.errorMessage); }}// called when a message arrivesfunction onMessageArrived(message) { console.log("onMessageArrived:"+message.payloadString);}
注意不要在client前写var
否则你可能会出现一下错误:转载地址:http://idgxf.baihongyu.com/