MQTTClient for Node.js
To start before, you need to know something about MQTT, please see MQTT V3.1 Protocol Specification
在使用本模块之前,你需要了解一些MQTT协议的知识,可参阅:MQTT V3.1 协议规范
Install 安装
npm install MQTTClient
Examples 示例
var MQTTClient = Client; // if you don't assigned a client_id, will automatically assigns one // 如果没有指定client_id,则程序会自动分配一个 var options = client_id: 'you_client_id' var client = 'localhost' 1883 options; client;
Subscribe & Un Subscribe 订阅和退订
// subscribe to a topic // 订阅一个主题 var options = dup_flag: 0 qos_level: 0 client; // Simplified: client.subscribe('topic_name'); // 也可以这样: client.subscribe('主题'); // un subscribe a topic // 退订一个主题 client; // Simplified: client.unSubscribe('topic_name'); // 也可以这样: client.unSubscribe('主题');
Publish 发布
// publish message to a topic // 发布一个消息到指定主题 var options = dup_flag: 0 qos_level: 0 retain: false client; // Simplified: client.publish('topic_name', 'payload'); // 也可以这样: client.publish('主题', '内容');
Other 其他
// send a PINGREQ to keep alive, will automatically be called // 发送一个PINGREQ消息给服务器,一般情况下会自动执行 client; // disconnect // 断开连接 client;
Event
connect
Connect to server success, after received a CONNACK message from the server
当连接服务器成功,并收到CONNACK消息后,触发此事件
Arguments: None
error
Has an error
当发生错误时触发此事件
Arguments: error
disconnect
The server close the socket connection
当服务器断开连接时触发此事件
Arguments: None
ping
After received a PINGRESP message from the server
当收到服务器返回的PINGRESP消息时触发此事件
Arguments: None
timeout
Not received the PINGRESP message out of options.alive_timer seconds
当超过指定时间(有options.alive_timer设置)没有收到服务器返回的PINGRESP消息时触发此事件
Arguments: None
publish
Received a PUBLISH message
当收到PUBLISH消息时触发此事件
Arguments: topic, payload, message_id
参数topic为消息的主题,payload为消息内容, message_id为消息ID