topic_publish.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // topic_publish.cpp
  2. //
  3. // This is a Paho MQTT C++ client, sample application.
  4. //
  5. // It's an example of how to send messages as an MQTT publisher using the
  6. // C++ asynchronous client interface using a 'topic' object to repeatedly
  7. // send data to the same topic.
  8. //
  9. // The sample demonstrates:
  10. // - Connecting to an MQTT server/broker
  11. // - Publishing messages
  12. // - Use of the 'topic' class
  13. //
  14. /*******************************************************************************
  15. * Copyright (c) 2019 Frank Pagliughi <fpagliughi@mindspring.com>
  16. *
  17. * All rights reserved. This program and the accompanying materials
  18. * are made available under the terms of the Eclipse Public License v1.0
  19. * and Eclipse Distribution License v1.0 which accompany this distribution.
  20. *
  21. * The Eclipse Public License is available at
  22. * http://www.eclipse.org/legal/epl-v10.html
  23. * and the Eclipse Distribution License is available at
  24. * http://www.eclipse.org/org/documents/edl-v10.php.
  25. *
  26. * Contributors:
  27. * Frank Pagliughi - initial implementation and documentation
  28. *******************************************************************************/
  29. #include <iostream>
  30. #include <cstdlib>
  31. #include <string>
  32. #include <thread> // For sleep
  33. #include <atomic>
  34. #include <chrono>
  35. #include <cstring>
  36. #include "mqtt/async_client.h"
  37. using namespace std;
  38. const string DFLT_SERVER_ADDRESS { "tcp://localhost:1883" };
  39. const string TOPIC { "test" };
  40. const int QOS = 1;
  41. const char* PAYLOADS[] = {
  42. "Hello World!",
  43. "Hi there!",
  44. "Is anyone listening?",
  45. "Someone is always listening.",
  46. nullptr
  47. };
  48. const auto TIMEOUT = std::chrono::seconds(10);
  49. /////////////////////////////////////////////////////////////////////////////
  50. int main(int argc, char* argv[])
  51. {
  52. string address = (argc > 1) ? string(argv[1]) : DFLT_SERVER_ADDRESS;
  53. cout << "Initializing for server '" << address << "'..." << endl;
  54. mqtt::async_client cli(address, "");
  55. cout << " ...OK" << endl;
  56. try {
  57. cout << "\nConnecting..." << endl;
  58. cli.connect()->wait();
  59. cout << " ...OK" << endl;
  60. cout << "\nPublishing messages..." << endl;
  61. mqtt::topic top(cli, "test", QOS);
  62. mqtt::token_ptr tok;
  63. size_t i = 0;
  64. while (PAYLOADS[i]) {
  65. tok = top.publish(PAYLOADS[i++]);
  66. }
  67. tok->wait(); // Just wait for the last one to complete.
  68. cout << "OK" << endl;
  69. // Disconnect
  70. cout << "\nDisconnecting..." << endl;
  71. cli.disconnect()->wait();
  72. cout << " ...OK" << endl;
  73. }
  74. catch (const mqtt::exception& exc) {
  75. cerr << exc.what() << endl;
  76. return 1;
  77. }
  78. return 0;
  79. }