1pubasync(3)                Library Functions Manual                pubasync(3)
2
3
4

NAME

6       pubasync - Asynchronous publication example
7
8
9       #include <stdio.h>
10       #include <stdlib.h>
11       #include <string.h>
12       #include "MQTTClient.h"
13
14       #if !defined(_WIN32)
15       #include <unistd.h>
16       #else
17       #include <windows.h>
18       #endif
19
20       #define ADDRESS     "tcp://mqtt.eclipse.org:1883"
21       #define CLIENTID    "ExampleClientPub"
22       #define TOPIC       "MQTT Examples"
23       #define PAYLOAD     "Hello World!"
24       #define QOS         1
25       #define TIMEOUT     10000L
26
27       MQTTClient_deliveryToken deliveredtoken;
28
29       void delivered(void *context, MQTTClient_deliveryToken dt)
30       {
31           printf("Message with token value %d delivery confirmed0, dt);
32           deliveredtoken = dt;
33       }
34
35       int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
36       {
37           printf("Message arrived0);
38           printf("     topic: %s0, topicName);
39           printf("   message: %.*s0, message->payloadlen, (char*)message->payload);
40           MQTTClient_freeMessage(&message);
41           MQTTClient_free(topicName);
42           return 1;
43       }
44
45       void connlost(void *context, char *cause)
46       {
47           printf("0onnection lost0);
48           printf("     cause: %s0, cause);
49       }
50
51       int main(int argc, char* argv[])
52       {
53           MQTTClient client;
54           MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
55           MQTTClient_message pubmsg = MQTTClient_message_initializer;
56           MQTTClient_deliveryToken token;
57           int rc;
58
59           if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
60               MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
61           {
62               printf("Failed to create client, return code %d0, rc);
63               rc = EXIT_FAILURE;
64               goto exit;
65           }
66
67           if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
68           {
69               printf("Failed to set callbacks, return code %d0, rc);
70               rc = EXIT_FAILURE;
71               goto destroy_exit;
72           }
73
74           conn_opts.keepAliveInterval = 20;
75           conn_opts.cleansession = 1;
76           if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
77           {
78               printf("Failed to connect, return code %d0, rc);
79               rc = EXIT_FAILURE;
80               goto destroy_exit;
81           }
82
83           pubmsg.payload = PAYLOAD;
84           pubmsg.payloadlen = (int)strlen(PAYLOAD);
85           pubmsg.qos = QOS;
86           pubmsg.retained = 0;
87           deliveredtoken = 0;
88           if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
89           {
90               printf("Failed to publish message, return code %d0, rc);
91               rc = EXIT_FAILURE;
92           }
93           else
94           {
95               printf("Waiting for publication of %s0
96                   "on topic %s for client with ClientID: %s0,
97                   PAYLOAD, TOPIC, CLIENTID);
98               while (deliveredtoken != token)
99               {
100                               #if defined(_WIN32)
101                                       Sleep(100);
102                               #else
103                                       usleep(10000L);
104                               #endif
105               }
106           }
107
108           if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
109           {
110               printf("Failed to disconnect, return code %d0, rc);
111               rc = EXIT_FAILURE;
112           }
113
114       destroy_exit:
115           MQTTClient_destroy(&client);
116
117       exit:
118           return rc;
119       }
120
121
122Paho MQTT C Client Library      Thu Jan 20 2022                    pubasync(3)
Impressum