| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> |
| <meta http-equiv="X-UA-Compatible" content="IE=9"/> |
| <meta name="generator" content="Doxygen 1.8.14"/> |
| <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| <title>Paho MQTT C Client Library: Asynchronous subscription example</title> |
| <link href="tabs.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript" src="jquery.js"></script> |
| <script type="text/javascript" src="dynsections.js"></script> |
| <link href="search/search.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript" src="search/searchdata.js"></script> |
| <script type="text/javascript" src="search/search.js"></script> |
| <link href="doxygen.css" rel="stylesheet" type="text/css" /> |
| </head> |
| <body> |
| <div id="top"><!-- do not remove this div, it is closed by doxygen! --> |
| <div id="titlearea"> |
| <table cellspacing="0" cellpadding="0"> |
| <tbody> |
| <tr style="height: 56px;"> |
| <td id="projectlogo"><img alt="Logo" src="pahologo.png"/></td> |
| <td id="projectalign" style="padding-left: 0.5em;"> |
| <div id="projectname">Paho MQTT C Client Library |
| </div> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| <!-- end header part --> |
| <!-- Generated by Doxygen 1.8.14 --> |
| <script type="text/javascript"> |
| /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ |
| var searchBox = new SearchBox("searchBox", "search",false,'Search'); |
| /* @license-end */ |
| </script> |
| <script type="text/javascript" src="menudata.js"></script> |
| <script type="text/javascript" src="menu.js"></script> |
| <script type="text/javascript"> |
| /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ |
| $(function() { |
| initMenu('',true,false,'search.php','Search'); |
| $(document).ready(function() { init_search(); }); |
| }); |
| /* @license-end */</script> |
| <div id="main-nav"></div> |
| <!-- window showing the filter options --> |
| <div id="MSearchSelectWindow" |
| onmouseover="return searchBox.OnSearchSelectShow()" |
| onmouseout="return searchBox.OnSearchSelectHide()" |
| onkeydown="return searchBox.OnSearchSelectKey(event)"> |
| </div> |
| |
| <!-- iframe showing the search results (closed by default) --> |
| <div id="MSearchResultsWindow"> |
| <iframe src="javascript:void(0)" frameborder="0" |
| name="MSearchResults" id="MSearchResults"> |
| </iframe> |
| </div> |
| |
| </div><!-- top --> |
| <div class="header"> |
| <div class="headertitle"> |
| <div class="title">Asynchronous subscription example </div> </div> |
| </div><!--header--> |
| <div class="contents"> |
| <div class="textblock"><div class="fragment"><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <stdlib.h></span></div><div class="line"><span class="preprocessor">#include <string.h></span></div><div class="line"><span class="preprocessor">#include "<a class="code" href="_m_q_t_t_client_8h.html">MQTTClient.h</a>"</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define ADDRESS "tcp://localhost:1883"</span></div><div class="line"><span class="preprocessor">#define CLIENTID "ExampleClientSub"</span></div><div class="line"><span class="preprocessor">#define TOPIC "MQTT Examples"</span></div><div class="line"><span class="preprocessor">#define PAYLOAD "Hello World!"</span></div><div class="line"><span class="preprocessor">#define QOS 1</span></div><div class="line"><span class="preprocessor">#define TIMEOUT 10000L</span></div><div class="line"></div><div class="line"><span class="keyword">volatile</span> <a class="code" href="_m_q_t_t_client_8h.html#a73e49030fd8b7074aa1aa45669b7fe8d">MQTTClient_deliveryToken</a> deliveredtoken;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> delivered(<span class="keywordtype">void</span> *context, <a class="code" href="_m_q_t_t_client_8h.html#a73e49030fd8b7074aa1aa45669b7fe8d">MQTTClient_deliveryToken</a> dt)</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">"Message with token value %d delivery confirmed\n"</span>, dt);</div><div class="line"> deliveredtoken = dt;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> msgarrvd(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *topicName, <span class="keywordtype">int</span> topicLen, <a class="code" href="struct_m_q_t_t_client__message.html">MQTTClient_message</a> *message)</div><div class="line">{</div><div class="line"> <span class="keywordtype">int</span> i;</div><div class="line"> <span class="keywordtype">char</span>* payloadptr;</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Message arrived\n"</span>);</div><div class="line"> printf(<span class="stringliteral">" topic: %s\n"</span>, topicName);</div><div class="line"> printf(<span class="stringliteral">" message: "</span>);</div><div class="line"></div><div class="line"> payloadptr = message-><a class="code" href="struct_m_q_t_t_client__message.html#a9eff55064941fb604452abb0050ea99d">payload</a>;</div><div class="line"> <span class="keywordflow">for</span>(i=0; i<message-><a class="code" href="struct_m_q_t_t_client__message.html#aa3cb44feb3ae6d11b3a4cad2d94cb33a">payloadlen</a>; i++)</div><div class="line"> {</div><div class="line"> putchar(*payloadptr++);</div><div class="line"> }</div><div class="line"> putchar(<span class="charliteral">'\n'</span>);</div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#abd8abde4f39d3e689029de27f7a98a65">MQTTClient_freeMessage</a>(&message);</div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#a203b545c999beb6b825ec99b6aea79ab">MQTTClient_free</a>(topicName);</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> connlost(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *cause)</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">"\nConnection lost\n"</span>);</div><div class="line"> printf(<span class="stringliteral">" cause: %s\n"</span>, cause);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#a7649e3913f9a216424d296f88a969c59">MQTTClient</a> client;</div><div class="line"> <a class="code" href="struct_m_q_t_t_client__connect_options.html">MQTTClient_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_client_8h.html#aefd7c865f2641c8155b763fdf3061c25">MQTTClient_connectOptions_initializer</a>;</div><div class="line"> <span class="keywordtype">int</span> rc;</div><div class="line"> <span class="keywordtype">int</span> ch;</div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#a9a0518d9ca924d12c1329dbe3de5f2b6">MQTTClient_create</a>(&client, ADDRESS, CLIENTID,</div><div class="line"> <a class="code" href="_m_q_t_t_client_persistence_8h.html#ae01e089313a65ac4661ed216b6ac00fa">MQTTCLIENT_PERSISTENCE_NONE</a>, NULL);</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#aad27d07782991a4937ebf2f39a021f83">MQTTClient_setCallbacks</a>(client, NULL, connlost, msgarrvd, delivered);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_client_8h.html#aaa8ae61cd65c9dc0846df10122d7bd4e">MQTTClient_connect</a>(client, &conn_opts)) != <a class="code" href="_m_q_t_t_client_8h.html#acba095704d79e5a1996389fa26203f73">MQTTCLIENT_SUCCESS</a>)</div><div class="line"> {</div><div class="line"> printf(<span class="stringliteral">"Failed to connect, return code %d\n"</span>, rc);</div><div class="line"> exit(EXIT_FAILURE);</div><div class="line"> }</div><div class="line"> printf(<span class="stringliteral">"Subscribing to topic %s\nfor client %s using QoS%d\n\n"</span></div><div class="line"> <span class="stringliteral">"Press Q<Enter> to quit\n\n"</span>, TOPIC, CLIENTID, QOS);</div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#a9c1c28258f0d5c6a44ff53a98618f5f3">MQTTClient_subscribe</a>(client, TOPIC, QOS);</div><div class="line"></div><div class="line"> <span class="keywordflow">do</span></div><div class="line"> {</div><div class="line"> ch = getchar();</div><div class="line"> } <span class="keywordflow">while</span>(ch!=<span class="charliteral">'Q'</span> && ch != <span class="charliteral">'q'</span>);</div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#a1e4d90c13a3c0705bc4a13bfe64e6525">MQTTClient_disconnect</a>(client, 10000);</div><div class="line"> <a class="code" href="_m_q_t_t_client_8h.html#ae700c3f5cfea3813264ce95e7c8cf498">MQTTClient_destroy</a>(&client);</div><div class="line"> <span class="keywordflow">return</span> rc;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents --> |
| <!-- start footer part --> |
| <hr class="footer"/><address class="footer"><small> |
| Generated on Thu Sep 13 2018 13:40:20 for Paho MQTT C Client Library by  <a href="http://www.doxygen.org/index.html"> |
| <img class="footer" src="doxygen.png" alt="doxygen"/> |
| </a> 1.8.14 |
| </small></address> |
| </body> |
| </html> |