wstring str = getAppPath(); CDllHelper dllMqtt; DWORD dwRet = dllMqtt.addDllSearchPath(str + L"ini"); dwRet = dllMqtt.loadDll(str + L"ini/paho-mqtt3c.dll"); if (dwRet) { ASSERT(FALSE); } auto pMQTTClient_create = dllMqtt.getDllFunc<int(MQTTClient*, const char*, const char*,int , void*)>("MQTTClient_create"); auto pMQTTClient_connect = dllMqtt.getDllFunc<int(MQTTClient, MQTTClient_connectOptions*)>("MQTTClient_connect"); auto pMQTTClient_destroy = dllMqtt.getDllFunc<void(MQTTClient*)>("MQTTClient_destroy"); auto pMQTTClient_disconnect = dllMqtt.getDllFunc<int(MQTTClient, int)>("MQTTClient_disconnect"); auto pMQTTClient_setCallbacks = dllMqtt.getDllFunc<int(MQTTClient handle, void* context, MQTTClient_connectionLost* cl, MQTTClient_messageArrived* ma, MQTTClient_deliveryComplete* dc)>("MQTTClient_setCallbacks"); auto pMQTTClient_subscribe = dllMqtt.getDllFunc<int(MQTTClient handle, const char* topic, int qos)>("MQTTClient_subscribe"); auto pMQTTClient_unsubscribe = dllMqtt.getDllFunc<int(MQTTClient handle, const char* topic)>("MQTTClient_unsubscribe");
#define ADDRESS "tcp://localhost:1883" #define CLIENTID "66889710-BA34-4BBC-811A-ABFC2AD281EB" #define TOPIC "event/#" #define PAYLOAD "Hello World!" #define QOS 1 #define TIMEOUT 10000L
MQTTClient client; MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; int rc; int ch;
int iRet = pMQTTClient_create(&client, ADDRESS, CLIENTID,MQTTCLIENT_PERSISTENCE_NONE, NULL); if (MQTTCLIENT_SUCCESS != iRet) { ASSERT(FALSE); } conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; conn_opts.connectTimeout = 2; conn_opts.username = "admin"; conn_opts.password = "123456"; conn_opts.MQTTVersion = MQTTVERSION_DEFAULT;
iRet = pMQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered); if (MQTTCLIENT_SUCCESS != iRet) { ASSERT(FALSE); } iRet = pMQTTClient_connect(client, &conn_opts); if (iRet != MQTTCLIENT_SUCCESS) { printf("Failed to connect, return code %d\n", iRet); exit(EXIT_FAILURE); } printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n" "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS); iRet = pMQTTClient_subscribe(client, TOPIC, QOS); if (MQTTCLIENT_SUCCESS != iRet) { ASSERT(FALSE); }
do { ch = getchar(); } while (ch != 'Q' && ch != 'q');
pMQTTClient_unsubscribe(client, TOPIC); pMQTTClient_disconnect(client, 10000); pMQTTClient_destroy(&client);
|