diff options
author | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-02-25 10:14:38 +0100 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2015-02-25 10:15:01 +0100 |
commit | fcd4c98922bd1a1a97ea264bd45e48cfa356c254 (patch) | |
tree | c01e6aee62e5c80c7d7f6f842de7bad3fbc46599 /examples | |
parent | b01068a406f6ebf51e103dd541f36fd6ef144805 (diff) | |
parent | fc2b42c874fb41487b86700a04e932e2afaded94 (diff) | |
download | qtwebsockets-fcd4c98922bd1a1a97ea264bd45e48cfa356c254.tar.gz |
Merge remote-tracking branch 'origin/5.4' into 5.5
Change-Id: I218b5069480f60b88390c8540f01c3dbbee485d0
Diffstat (limited to 'examples')
-rw-r--r-- | examples/websockets/echoserver/echoserver.cpp | 15 | ||||
-rw-r--r-- | examples/websockets/echoserver/echoserver.h | 3 | ||||
-rw-r--r-- | examples/websockets/echoserver/main.cpp | 20 |
3 files changed, 32 insertions, 6 deletions
diff --git a/examples/websockets/echoserver/echoserver.cpp b/examples/websockets/echoserver/echoserver.cpp index f83a1ea..8c785e8 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,10 +78,11 @@ 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); } - m_pWebSocketServer->close(); } //! [processTextMessage] @@ -87,6 +90,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 +102,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 a8c860e..2bb9067 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 3b31dde..51a684a 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(); |