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

NAME

6       subscribe - Subscription example
7
8
9       #include <stdio.h>
10       #include <stdlib.h>
11       #include <string.h>
12       #include "MQTTAsync.h"
13
14       #if !defined(_WIN32)
15       #include <unistd.h>
16       #else
17       #include <windows.h>
18       #endif
19
20       #if defined(_WRS_KERNEL)
21       #include <OsWrapper.h>
22       #endif
23
24       #define ADDRESS     "tcp://mqtt.eclipse.org:1883"
25       #define CLIENTID    "ExampleClientSub"
26       #define TOPIC       "MQTT Examples"
27       #define PAYLOAD     "Hello World!"
28       #define QOS         1
29       #define TIMEOUT     10000L
30
31       int disc_finished = 0;
32       int subscribed = 0;
33       int finished = 0;
34
35       void connlost(void *context, char *cause)
36       {
37               MQTTAsync client = (MQTTAsync)context;
38               MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
39               int rc;
40
41               printf("0onnection lost0);
42               if (cause)
43                       printf("     cause: %s0, cause);
44
45               printf("Reconnecting0);
46               conn_opts.keepAliveInterval = 20;
47               conn_opts.cleansession = 1;
48               if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
49               {
50                       printf("Failed to start connect, return code %d0, rc);
51                       finished = 1;
52               }
53       }
54
55
56       int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
57       {
58           printf("Message arrived0);
59           printf("     topic: %s0, topicName);
60           printf("   message: %.*s0, message->payloadlen, (char*)message->payload);
61           MQTTAsync_freeMessage(&message);
62           MQTTAsync_free(topicName);
63           return 1;
64       }
65
66       void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
67       {
68               printf("Disconnect failed, rc %d0, response->code);
69               disc_finished = 1;
70       }
71
72       void onDisconnect(void* context, MQTTAsync_successData* response)
73       {
74               printf("Successful disconnection0);
75               disc_finished = 1;
76       }
77
78       void onSubscribe(void* context, MQTTAsync_successData* response)
79       {
80               printf("Subscribe succeeded0);
81               subscribed = 1;
82       }
83
84       void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
85       {
86               printf("Subscribe failed, rc %d0, response->code);
87               finished = 1;
88       }
89
90
91       void onConnectFailure(void* context, MQTTAsync_failureData* response)
92       {
93               printf("Connect failed, rc %d0, response->code);
94               finished = 1;
95       }
96
97
98       void onConnect(void* context, MQTTAsync_successData* response)
99       {
100               MQTTAsync client = (MQTTAsync)context;
101               MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
102               int rc;
103
104               printf("Successful connection0);
105
106               printf("Subscribing to topic %s0or client %s using QoS%d0
107                  "Press Q<Enter> to quit0, TOPIC, CLIENTID, QOS);
108               opts.onSuccess = onSubscribe;
109               opts.onFailure = onSubscribeFailure;
110               opts.context = client;
111               if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
112               {
113                       printf("Failed to start subscribe, return code %d0, rc);
114                       finished = 1;
115               }
116       }
117
118
119       int main(int argc, char* argv[])
120       {
121               MQTTAsync client;
122               MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
123               MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
124               int rc;
125               int ch;
126
127               if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
128                               != MQTTASYNC_SUCCESS)
129               {
130                       printf("Failed to create client, return code %d0, rc);
131                       rc = EXIT_FAILURE;
132                       goto exit;
133               }
134
135               if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
136               {
137                       printf("Failed to set callbacks, return code %d0, rc);
138                       rc = EXIT_FAILURE;
139                       goto destroy_exit;
140               }
141
142               conn_opts.keepAliveInterval = 20;
143               conn_opts.cleansession = 1;
144               conn_opts.onSuccess = onConnect;
145               conn_opts.onFailure = onConnectFailure;
146               conn_opts.context = client;
147               if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
148               {
149                       printf("Failed to start connect, return code %d0, rc);
150                       rc = EXIT_FAILURE;
151                       goto destroy_exit;
152               }
153
154               while (!subscribed && !finished)
155                       #if defined(_WIN32)
156                               Sleep(100);
157                       #else
158                               usleep(10000L);
159                       #endif
160
161               if (finished)
162                       goto exit;
163
164               do
165               {
166                       ch = getchar();
167               } while (ch!='Q' && ch != 'q');
168
169               disc_opts.onSuccess = onDisconnect;
170               disc_opts.onFailure = onDisconnectFailure;
171               if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
172               {
173                       printf("Failed to start disconnect, return code %d0, rc);
174                       rc = EXIT_FAILURE;
175                       goto destroy_exit;
176               }
177               while (!disc_finished)
178               {
179                       #if defined(_WIN32)
180                               Sleep(100);
181                       #else
182                               usleep(10000L);
183                       #endif
184               }
185
186       destroy_exit:
187               MQTTAsync_destroy(&client);
188       exit:
189               return rc;
190       }
191
192
193Paho Asynchronous MQTT C Client LTihburaJruyl 22 2021                   subscribe(3)
Impressum