diff options
author | Liang Qi <liang.qi@theqtcompany.com> | 2015-02-13 11:31:52 +0100 |
---|---|---|
committer | Liang Qi <liang.qi@theqtcompany.com> | 2015-02-20 11:30:08 +0000 |
commit | 8ebed733e872ded507256addf932e19e1b6ed2b6 (patch) | |
tree | ddb93e50d4a33468ce4d2cfab503fb060c39e39c | |
parent | 85a8ea105646c7d871f982b890ef5f6faa91824d (diff) | |
download | qtwebsockets-8ebed733e872ded507256addf932e19e1b6ed2b6.tar.gz |
Examples: add options for port and debug outputs in echoserver
Change-Id: Iadba1c5cb7cd4454b01a98339c2225483b08e180
Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
Reviewed-by: Venugopal Shivashankar <venugopal.shivashankar@digia.com>
Reviewed-by: Topi Reiniƶ <topi.reinio@digia.com>
-rw-r--r-- | examples/websockets/echoserver/echoserver.cpp | 14 | ||||
-rw-r--r-- | examples/websockets/echoserver/echoserver.h | 3 | ||||
-rw-r--r-- | examples/websockets/echoserver/main.cpp | 20 |
3 files changed, 32 insertions, 5 deletions
diff --git a/examples/websockets/echoserver/echoserver.cpp b/examples/websockets/echoserver/echoserver.cpp index b3ea177..a3109fa 100644 --- a/examples/websockets/echoserver/echoserver.cpp +++ b/examples/websockets/echoserver/echoserver.cpp @@ -38,14 +38,16 @@ QT_USE_NAMESPACE //! [constructor] -EchoServer::EchoServer(quint16 port, QObject *parent) : +EchoServer::EchoServer(quint16 port, bool debug, QObject *parent) : QObject(parent), m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), QWebSocketServer::NonSecureMode, this)), - m_clients() + m_clients(), + m_debug(debug) { if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { - qDebug() << "Echoserver listening on port" << port; + if (m_debug) + qDebug() << "Echoserver listening on port" << port; connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &EchoServer::onNewConnection); connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &EchoServer::closed); @@ -76,6 +78,8 @@ void EchoServer::onNewConnection() void EchoServer::processTextMessage(QString message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (m_debug) + qDebug() << "Message received:" << message; if (pClient) { pClient->sendTextMessage(message); } @@ -87,6 +91,8 @@ void EchoServer::processTextMessage(QString message) void EchoServer::processBinaryMessage(QByteArray message) { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (m_debug) + qDebug() << "Binary Message received:" << message; if (pClient) { pClient->sendBinaryMessage(message); } @@ -97,6 +103,8 @@ void EchoServer::processBinaryMessage(QByteArray message) void EchoServer::socketDisconnected() { QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); + if (m_debug) + qDebug() << "socketDisconnected:" << pClient; if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); diff --git a/examples/websockets/echoserver/echoserver.h b/examples/websockets/echoserver/echoserver.h index 7ce57cc..4559ee2 100644 --- a/examples/websockets/echoserver/echoserver.h +++ b/examples/websockets/echoserver/echoserver.h @@ -44,7 +44,7 @@ class EchoServer : public QObject { Q_OBJECT public: - explicit EchoServer(quint16 port, QObject *parent = Q_NULLPTR); + explicit EchoServer(quint16 port, bool debug = false, QObject *parent = Q_NULLPTR); ~EchoServer(); Q_SIGNALS: @@ -59,6 +59,7 @@ private Q_SLOTS: private: QWebSocketServer *m_pWebSocketServer; QList<QWebSocket *> m_clients; + bool m_debug; }; #endif //ECHOSERVER_H diff --git a/examples/websockets/echoserver/main.cpp b/examples/websockets/echoserver/main.cpp index ed408ae..c433030 100644 --- a/examples/websockets/echoserver/main.cpp +++ b/examples/websockets/echoserver/main.cpp @@ -31,12 +31,30 @@ ** ****************************************************************************/ #include <QtCore/QCoreApplication> +#include <QtCore/QCommandLineParser> +#include <QtCore/QCommandLineOption> #include "echoserver.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); - EchoServer *server = new EchoServer(1234); + + QCommandLineParser parser; + parser.setApplicationDescription("QtWebSockets example: echoserver"); + parser.addHelpOption(); + + QCommandLineOption dbgOption(QStringList() << "d" << "debug", + QCoreApplication::translate("main", "Debug output [default: off].")); + parser.addOption(dbgOption); + QCommandLineOption portOption(QStringList() << "p" << "port", + QCoreApplication::translate("main", "Port for echoserver [default: 1234]."), + QCoreApplication::translate("main", "port"), QLatin1Literal("1234")); + parser.addOption(portOption); + parser.process(a); + bool debug = parser.isSet(dbgOption); + int port = parser.value(portOption).toInt(); + + EchoServer *server = new EchoServer(port, debug); QObject::connect(server, &EchoServer::closed, &a, &QCoreApplication::quit); return a.exec(); |