附件包含库文件,demo演示文件。
以下为演示代码:
import win.ui;
/*DSG{{*/
mainForm = win.form(text="mqttdemo";right=415;bottom=526)
mainForm.add(
edit={cls="edit";text="Edit";left=18;top=24;right=390;bottom=353;bgcolor=0;color=65280;edge=1;font=LOGFONT(h=-14;name='微软雅黑');multiline=1;z=1}
)
/*}}*/
import mqttlib;
import console;
var mqtt = {
connect = function(addr, cid, fn){
if !mqttcli {
mqttcli = mqttlib.MQTT(mainForm, $"\lib\mqtt.dll");
}
mqttcli.connect(addr, cid, fn);
};
sub = function(topic) {
mqttcli.sub(topic);
};
pub = function(topic, message) {
mqttcli.pub(topic, message);
}
};
mqtt.connect("127.0.0.1:1983", "somecid", function(topic, msg) {
if topic == "$" {
mainForm.edit.print("已连接mqtt broker");
return;
}
mainForm.edit.printf("recv: <- %s : %s", topic, msg);
});
mqtt.sub("topic0");
mainForm.edit.print("> 订阅topic0")
mqtt.sub("topic1");
mainForm.edit.print("> 订阅topic1")
mainForm.edit.print("> publish topic0")
mqtt.pub("topic0", "hello world!");
mainForm.edit.print("> publish topic1")
mqtt.pub("topic1", "fuck the world!");
mainForm.show();
return win.loopMessage();