开阔未来
全部博文(102)
分类: 系统运维
2011-12-23 17:13:36
Limitation:
1. Only do QOs1 messaging.
2. Can not handle payloads larger than 128 byte.
3. Length for subscribe topic is limited to 128 bytes.
Example:
This
is simple program called 'loader.js', which create a MQTTClient
instance and send “here is nodejs” to “node” after a session is opened
with server. Then it display incoming data from topic “/mirror”.
var sys = require('sys');
var net = require('net');
var mqtt = require('./mqtt');
var client = new mqtt.MQTTClient(1883, '127.0.0.1', 'mirror');
client.addListener('sessionOpened', function(){
client.subscribe('/mirror');
client.publish('node', 'here is nodejs');
});
client.addListener('mqttData', function(topic, payload){
sys.puts(topic+':'+payload);
});
API
Event: ‘sessionOpened’
Fired when a session with broker is opened sucesffully.
Event: ‘mqttData’
Fired when data is available for client, topic and payload have been extracted from data.
client.addListener(‘mqttData’, function(topic, payload){
});
Event: ‘openSessionFailed’
Fired when a session can not be established.
Event: ‘connectTimeOut’
Fired when cant establish a connection with server.
MQTTClient(port, host, clientID);
Construct a instance of mqtt client.
@port: port number, like 1883.
@host: server ip address.
@clientID: client name for server.
subscribe(sub_topic);
Subscribe to a topic.
@sub_topic: topic to be subscribed.
publish(pub_topic, msg);
Publish messages to a topic.
@pub_topic: publish topics.
@msg: payload data, can be anything, string, bytes.