diff options
Diffstat (limited to 'tests/manual/wasm/network/echo_client_mainthread')
-rw-r--r-- | tests/manual/wasm/network/echo_client_mainthread/CMakeLists.txt | 10 | ||||
-rw-r--r-- | tests/manual/wasm/network/echo_client_mainthread/main.cpp | 52 |
2 files changed, 62 insertions, 0 deletions
diff --git a/tests/manual/wasm/network/echo_client_mainthread/CMakeLists.txt b/tests/manual/wasm/network/echo_client_mainthread/CMakeLists.txt new file mode 100644 index 0000000000..4b81661c79 --- /dev/null +++ b/tests/manual/wasm/network/echo_client_mainthread/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (C) 2024 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause +qt_internal_add_manual_test(echo_client_mainthread + GUI + SOURCES + main.cpp + LIBRARIES + Qt::Core + Qt::Network +) diff --git a/tests/manual/wasm/network/echo_client_mainthread/main.cpp b/tests/manual/wasm/network/echo_client_mainthread/main.cpp new file mode 100644 index 0000000000..ef696e5978 --- /dev/null +++ b/tests/manual/wasm/network/echo_client_mainthread/main.cpp @@ -0,0 +1,52 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtCore> +#include <QtNetwork> + +int main(int argc, char **argv) { + + QCoreApplication app(argc, argv); + + // This example connects to localhost, but note that the host can + // be any host reachable from the client using webscokets, at any port. + QString hostName = "localhost"; + int port = 1515; + qDebug() << "This example connects to a server at" << hostName << "port" << port << "," + << "where it expects to find a WebSockify server, which forwards to the fortune server."; + + auto echo = [hostName, port]() { + QTcpSocket *socket = new QTcpSocket(); + + QObject::connect(socket, &QAbstractSocket::connected, [socket]() { + qDebug() << "Connected"; + socket->write("Hello, echo server!"); + socket->flush(); + }); + + QObject::connect(socket, &QIODevice::readyRead, [socket]() { + QByteArray data = socket->readAll(); + qDebug() << "Ready Read, got echo:" << data; + socket->disconnectFromHost(); + socket->deleteLater(); + }); + + QObject::connect(socket, &QAbstractSocket::errorOccurred, [socket]() { + qDebug() << "Error Occurred" << socket->error(); + }); + + QObject::connect(socket, &QAbstractSocket::disconnected, [socket]() { + qDebug() << "Disconnected"; + socket->deleteLater(); + }); + + qDebug() << "Connect to host" << hostName << port; + socket->connectToHost(hostName, port); + }; + + QTimer::singleShot(500, [echo](){ + echo(); + }); + + return app.exec(); +} |