diff options
author | Øystein Heskestad <oystein.heskestad@qt.io> | 2023-02-22 18:05:41 +0100 |
---|---|---|
committer | Øystein Heskestad <oystein.heskestad@qt.io> | 2023-03-24 14:45:57 +0100 |
commit | 99fdf11b808ab9b38239b04399a3fff21fa6ed35 (patch) | |
tree | 53184ab9bd7bdbfe39c991faa6fff12c382b93f0 /examples/websockets/echoclient/main.cpp | |
parent | 12d84ebf5f205498c14e1bfb0174a4982c28337f (diff) |
Make endpoint configurable for echoclient and sslechoclient examples
Add handling of command line arguments for hostname and port.
Task-number: QTBUG-110894
Pick-to: 6.5
Change-Id: I22d8f9e112cfb6c02b3e741c14720a0f28565984
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'examples/websockets/echoclient/main.cpp')
-rw-r--r-- | examples/websockets/echoclient/main.cpp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/examples/websockets/echoclient/main.cpp b/examples/websockets/echoclient/main.cpp index 5841610..999df79 100644 --- a/examples/websockets/echoclient/main.cpp +++ b/examples/websockets/echoclient/main.cpp @@ -7,19 +7,41 @@ int main(int argc, char *argv[]) { + using namespace Qt::Literals::StringLiterals; QCoreApplication a(argc, argv); QCommandLineParser parser; parser.setApplicationDescription("QtWebSockets example: echoclient"); parser.addHelpOption(); - QCommandLineOption dbgOption(QStringList() << "d" << "debug", + QCommandLineOption dbgOption( + QStringList{ u"d"_s, u"debug"_s }, QCoreApplication::translate("main", "Debug output [default: off].")); parser.addOption(dbgOption); + QCommandLineOption hostnameOption( + QStringList{ u"n"_s, u"hostname"_s }, + QCoreApplication::translate("main", "Hostname [default: localhost]."), "hostname", + "localhost"); + parser.addOption(hostnameOption); + QCommandLineOption portOption(QStringList{ u"p"_s, u"port"_s }, + QCoreApplication::translate("main", "Port [default: 1234]."), + "port", "1234"); + parser.addOption(portOption); + parser.process(a); bool debug = parser.isSet(dbgOption); - - EchoClient client(QUrl(QStringLiteral("ws://localhost:1234")), debug); + bool ok = true; + int port = parser.value(portOption).toInt(&ok); + if (!ok || port < 1 || port > 65535) { + qWarning("Port invalid, must be a number between 1 and 65535\n%s", + qPrintable(parser.helpText())); + return 1; + } + QUrl url; + url.setScheme(u"ws"_s); + url.setHost(parser.value(hostnameOption)); + url.setPort(port); + EchoClient client(url, debug); QObject::connect(&client, &EchoClient::closed, &a, &QCoreApplication::quit); return a.exec(); |