mirror of https://github.com/eclipse/paho.mqtt.c
74 lines
5.5 KiB
HTML
74 lines
5.5 KiB
HTML
<!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"/>
|
|
<title>MQTT Client: Synchronous publication example</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<!-- Generated by Doxygen 1.6.3 -->
|
|
<div class="navigation" id="top">
|
|
<div class="tabs">
|
|
<ul>
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="annotated.html"><span>Data Structures</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="contents">
|
|
|
|
|
|
<h1><a class="anchor" id="pubsync">Synchronous publication example </a></h1><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "stdio.h"</span>
|
|
<span class="preprocessor">#include "stdlib.h"</span>
|
|
<span class="preprocessor">#include "string.h"</span>
|
|
<span class="preprocessor">#include "<a class="code" href="_m_q_t_t_client_8h.html">MQTTClient.h</a>"</span>
|
|
|
|
<span class="preprocessor">#define ADDRESS "tcp://localhost:1883"</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define CLIENTID "ExampleClientPub"</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define TOPIC "MQTT Examples"</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define PAYLOAD "Hello World!"</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define QOS 1</span>
|
|
<span class="preprocessor"></span><span class="preprocessor">#define TIMEOUT 10000L</span>
|
|
<span class="preprocessor"></span>
|
|
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])
|
|
{
|
|
<a class="code" href="_m_q_t_t_client_8h.html#a7649e3913f9a216424d296f88a969c59">MQTTClient</a> client;
|
|
<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>;
|
|
<a class="code" href="struct_m_q_t_t_client__message.html">MQTTClient_message</a> pubmsg = <a class="code" href="_m_q_t_t_client_8h.html#aa1fd995924d3df75959fcf57e87aefac">MQTTClient_message_initializer</a>;
|
|
<a class="code" href="_m_q_t_t_client_8h.html#a73e49030fd8b7074aa1aa45669b7fe8d">MQTTClient_deliveryToken</a> token;
|
|
<span class="keywordtype">int</span> rc;
|
|
|
|
<a class="code" href="_m_q_t_t_client_8h.html#a5cb44bc0e06bcc95a314d51320a0cd1b">MQTTClient_create</a>(&client, ADDRESS, CLIENTID,
|
|
<a class="code" href="_m_q_t_t_client_persistence_8h.html#ae01e089313a65ac4661ed216b6ac00fa">MQTTCLIENT_PERSISTENCE_NONE</a>, NULL);
|
|
conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;
|
|
conn_opts.<a class="code" href="struct_m_q_t_t_client__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;
|
|
|
|
<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>)
|
|
{
|
|
printf(<span class="stringliteral">"Failed to connect, return code %d\n"</span>, rc);
|
|
exit(-1);
|
|
}
|
|
pubmsg.<a class="code" href="struct_m_q_t_t_client__message.html#a9eff55064941fb604452abb0050ea99d">payload</a> = PAYLOAD;
|
|
pubmsg.<a class="code" href="struct_m_q_t_t_client__message.html#aa3cb44feb3ae6d11b3a4cad2d94cb33a">payloadlen</a> = strlen(PAYLOAD);
|
|
pubmsg.<a class="code" href="struct_m_q_t_t_client__message.html#a35738099155a0e4f54050da474bab2e7">qos</a> = QOS;
|
|
pubmsg.<a class="code" href="struct_m_q_t_t_client__message.html#a6a4904c112507a43e7dc8495b62cc0fc">retained</a> = 0;
|
|
<a class="code" href="_m_q_t_t_client_8h.html#a288d6c8d4919f06e991be2435f649676">MQTTClient_publishMessage</a>(client, TOPIC, &pubmsg, &token);
|
|
printf(<span class="stringliteral">"Waiting for up to %d seconds for publication of %s\n"</span>
|
|
<span class="stringliteral">"on topic %s for client with ClientID: %s\n"</span>,
|
|
(<span class="keywordtype">int</span>)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
|
|
rc = <a class="code" href="_m_q_t_t_client_8h.html#a83807ec81fe8c3941e368ab329d43067">MQTTClient_waitForCompletion</a>(client, token, TIMEOUT);
|
|
printf(<span class="stringliteral">"Message with delivery token %d delivered\n"</span>, token);
|
|
<a class="code" href="_m_q_t_t_client_8h.html#a1e4d90c13a3c0705bc4a13bfe64e6525">MQTTClient_disconnect</a>(client, 10000);
|
|
<a class="code" href="_m_q_t_t_client_8h.html#ae700c3f5cfea3813264ce95e7c8cf498">MQTTClient_destroy</a>(&client);
|
|
<span class="keywordflow">return</span> rc;
|
|
}
|
|
</pre></div> </div>
|
|
<hr class="footer"/><address style="text-align: right;"><small>Generated on Tue Jul 3 15:17:14 2012 for MQTT Client by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
|
|
</body>
|
|
</html>
|