1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// mqttpp_chat.cpp
//
// This is a Paho MQTT C++ client, sample application.
//
// The "chat" application is practically the "Hello World" application for
// messaging systems. This allows a user to type in message to send to a
// "group" while seeing all the messages that the other members of the group
// send.
//
// This application is an MQTT publisher/subscriber using the C++
// asynchronous client interface, employing callbacks to receive messages
// and status updates.
//
// The sample demonstrates:
// - Connecting to an MQTT server/broker.
// - Publishing messages.
// - Subscribing to a topic
// - Receiving messages (callbacks) through a lambda function
//
// USAGE:
// mqttpp_chat <user> <group>
/*******************************************************************************
* Copyright (c) 2019-2023 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <thread>
#include "mqtt/async_client.h"
#include "mqtt/topic.h"
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
// The broker/server address
const std::string SERVER_ADDRESS("mqtt://localhost:1883");
// The QoS to use for publishing and subscribing
const int QOS = 1;
// Tell the broker we don't want our own messages sent back to us.
const bool NO_LOCAL = true;
if (argc != 3) {
std::cout << "USAGE: mqttpp_chat <user> <group>" << std::endl;
return 1;
}
std::string chatUser{argv[1]}, chatGroup{argv[2]}, chatTopic{"chat/" + chatGroup};
mqtt::async_client cli(SERVER_ADDRESS);
// LWT message is broadcast to other users if out connection is lost
auto lwt =
mqtt::message(chatTopic, "<<<" + chatUser + " was disconnected>>>", QOS, false);
// Set up the connect options
auto connOpts = mqtt::connect_options_builder::v5()
.properties({{mqtt::property::SESSION_EXPIRY_INTERVAL, 604800}})
.clean_start(false)
.will(std::move(lwt))
.keep_alive_interval(std::chrono::seconds(20))
.finalize();
// Set a callback for connection lost.
// This just exits the app.
cli.set_connection_lost_handler([](const std::string&) {
std::cout << "*** Connection Lost ***" << std::endl;
exit(2);
});
// Set the callback for incoming messages
cli.set_message_callback([](mqtt::const_message_ptr msg) {
std::cout << msg->get_payload_str() << std::endl;
});
// We publish and subscribe to one topic,
// so a 'topic' object is helpful.
mqtt::topic topic{cli, "chat/" + chatGroup, QOS};
// Start the connection.
try {
std::cout << "Connecting to the chat server at '" << SERVER_ADDRESS << "'..."
<< std::flush;
auto tok = cli.connect(connOpts);
tok->wait();
// Subscribe to the topic using "no local" so that
// we don't get own messages sent back to us
std::cout << "Ok\nJoining the group..." << std::flush;
auto subOpts = mqtt::subscribe_options(NO_LOCAL);
topic.subscribe(subOpts)->wait();
std::cout << "Ok" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << "\nERROR: Unable to connect. " << exc.what() << std::endl;
return 1;
}
// Let everyone know that a new user joined the conversation.
topic.publish("<<" + chatUser + " joined the group>>");
// Read messages from the console and publish them.
// Quit when the use enters an empty line.
std::string usrMsg;
while (std::getline(std::cin, usrMsg) && !usrMsg.empty()) {
usrMsg = chatUser + ": " + usrMsg;
topic.publish(usrMsg);
}
// Let everyone know that the user left the conversation.
topic.publish("<<" + chatUser + " left the group>>")->wait();
// Disconnect
try {
std::cout << "Disconnecting from the chat server..." << std::flush;
cli.disconnect()->wait();
std::cout << "OK" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << exc.what() << std::endl;
return 1;
}
return 0;
}
|