diff options
author | Liang Qi <liang.qi@theqtcompany.com> | 2015-02-16 14:51:41 +0100 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-02-17 12:59:47 +0000 |
commit | 1bbba5b9e8761fabe79a212e221102503fb0ac25 (patch) | |
tree | 2819879f98063497e9961166ee456227323c0c9f /examples/websockets | |
parent | e0521c794bd1a4325d0c59e8f75aa263efa671d5 (diff) | |
download | qtwebsockets-1bbba5b9e8761fabe79a212e221102503fb0ac25.tar.gz |
Examples: add an option for debug outputs in echoclient
Change-Id: I9bec2324b666c00212135ef6c9d5a69594cda050
Reviewed-by: Topi Reiniƶ <topi.reinio@digia.com>
Diffstat (limited to 'examples/websockets')
-rw-r--r-- | examples/websockets/echoclient/echoclient.cpp | 13 | ||||
-rw-r--r-- | examples/websockets/echoclient/echoclient.h | 3 | ||||
-rw-r--r-- | examples/websockets/echoclient/main.cpp | 15 |
3 files changed, 25 insertions, 6 deletions
diff --git a/examples/websockets/echoclient/echoclient.cpp b/examples/websockets/echoclient/echoclient.cpp index 229ee1b..0e7b3c9 100644 --- a/examples/websockets/echoclient/echoclient.cpp +++ b/examples/websockets/echoclient/echoclient.cpp @@ -36,10 +36,13 @@ QT_USE_NAMESPACE //! [constructor] -EchoClient::EchoClient(const QUrl &url, QObject *parent) : +EchoClient::EchoClient(const QUrl &url, bool debug, QObject *parent) : QObject(parent), - m_url(url) + m_url(url), + m_debug(debug) { + if (m_debug) + qDebug() << "WebSocket server:" << url; connect(&m_webSocket, &QWebSocket::connected, this, &EchoClient::onConnected); connect(&m_webSocket, &QWebSocket::disconnected, this, &EchoClient::closed); m_webSocket.open(QUrl(url)); @@ -49,7 +52,8 @@ EchoClient::EchoClient(const QUrl &url, QObject *parent) : //! [onConnected] void EchoClient::onConnected() { - qDebug() << "WebSocket connected"; + if (m_debug) + qDebug() << "WebSocket connected"; connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &EchoClient::onTextMessageReceived); m_webSocket.sendTextMessage(QStringLiteral("Hello, world!")); @@ -59,7 +63,8 @@ void EchoClient::onConnected() //! [onTextMessageReceived] void EchoClient::onTextMessageReceived(QString message) { - qDebug() << "Message received:" << message; + if (m_debug) + qDebug() << "Message received:" << message; m_webSocket.close(); } //! [onTextMessageReceived] diff --git a/examples/websockets/echoclient/echoclient.h b/examples/websockets/echoclient/echoclient.h index 2ddef4c..dc005dc 100644 --- a/examples/websockets/echoclient/echoclient.h +++ b/examples/websockets/echoclient/echoclient.h @@ -40,7 +40,7 @@ class EchoClient : public QObject { Q_OBJECT public: - explicit EchoClient(const QUrl &url, QObject *parent = Q_NULLPTR); + explicit EchoClient(const QUrl &url, bool debug = false, QObject *parent = Q_NULLPTR); Q_SIGNALS: void closed(); @@ -52,6 +52,7 @@ private Q_SLOTS: private: QWebSocket m_webSocket; QUrl m_url; + bool m_debug; }; #endif // ECHOCLIENT_H diff --git a/examples/websockets/echoclient/main.cpp b/examples/websockets/echoclient/main.cpp index cdc6d04..911c069 100644 --- a/examples/websockets/echoclient/main.cpp +++ b/examples/websockets/echoclient/main.cpp @@ -31,12 +31,25 @@ ** ****************************************************************************/ #include <QtCore/QCoreApplication> +#include <QtCore/QCommandLineParser> +#include <QtCore/QCommandLineOption> #include "echoclient.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); - EchoClient client(QUrl(QStringLiteral("ws://localhost:1234"))); + + QCommandLineParser parser; + parser.setApplicationDescription("QtWebSockets example: echoclient"); + parser.addHelpOption(); + + QCommandLineOption dbgOption(QStringList() << "d" << "debug", + QCoreApplication::translate("main", "Debug output [default: off].")); + parser.addOption(dbgOption); + parser.process(a); + bool debug = parser.isSet(dbgOption); + + EchoClient client(QUrl(QStringLiteral("ws://localhost:1234")), debug); QObject::connect(&client, &EchoClient::closed, &a, &QCoreApplication::quit); return a.exec(); |