summaryrefslogtreecommitdiff
path: root/examples/standalone/main.cpp
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-07-03 14:57:03 +0200
committerMilian Wolff <milian.wolff@kdab.com>2014-07-04 20:19:27 +0200
commit20937e9192c52630b9b47765b58493ada64fcf65 (patch)
treead21560ea9eec538fab10abe7b6f70bc2c9d3c17 /examples/standalone/main.cpp
parent856dc072acb649be03e02ac64c81015d3f2675c2 (diff)
downloadqtwebchannel-20937e9192c52630b9b47765b58493ada64fcf65.tar.gz
Only depend optionally on QtWebSockets, and only use it in the example.
The utility QWebChannelAbstractTransport implementation based on the QtWebSocket has no big value. Instead, it would pull in the QtWebSocket link-time dependency into QtWebKit/QtWebEngine, which is not desired. Considering that the WebSocket usecase is minor, and only few people will ever use it, we agreed that having the code in the example alone is enough. Change-Id: Ica038329a1d684f33e805fc296e9dff71b1446ba Reviewed-by: Jocelyn Turcotte <jocelyn.turcotte@digia.com> Reviewed-by: Pierre Rossi <pierre.rossi@gmail.com>
Diffstat (limited to 'examples/standalone/main.cpp')
-rw-r--r--examples/standalone/main.cpp73
1 files changed, 26 insertions, 47 deletions
diff --git a/examples/standalone/main.cpp b/examples/standalone/main.cpp
index 05deb24..f407da6 100644
--- a/examples/standalone/main.cpp
+++ b/examples/standalone/main.cpp
@@ -49,8 +49,9 @@
#include <QDebug>
#include <QtWebSockets/QWebSocketServer>
-#include <QtWebSockets/QWebSocket>
-#include <QtWebChannel/QWebChannelWebSocketTransport>
+
+#include "websocketclientwrapper.h"
+#include "websockettransport.h"
#include "ui_dialog.h"
@@ -59,18 +60,18 @@ class Dialog : public QObject
Q_OBJECT
public:
- explicit Dialog(const QString &baseUrl, QObject *parent = 0)
+ explicit Dialog(QObject *parent = 0)
: QObject(parent)
{
ui.setupUi(&dialog);
dialog.show();
connect(ui.send, SIGNAL(clicked()), SLOT(clicked()));
+ }
- QUrl url = QUrl::fromLocalFile(SOURCE_DIR "/index.html");
- url.setQuery(QStringLiteral("webChannelBaseUrl=") + baseUrl);
- ui.output->appendPlainText(tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString()));
- QDesktopServices::openUrl(url);
+ void displayMessage(const QString &message)
+ {
+ ui.output->appendPlainText(message);
}
signals:
@@ -79,7 +80,7 @@ signals:
public slots:
void receiveText(const QString &text)
{
- ui.output->appendPlainText(tr("Received message: %1").arg(text));
+ displayMessage(tr("Received message: %1").arg(text));
}
private slots:
@@ -92,7 +93,7 @@ private slots:
}
emit sendText(text);
- ui.output->appendPlainText(tr("Sent message: %1").arg(text));
+ displayMessage(tr("Sent message: %1").arg(text));
ui.input->clear();
}
@@ -102,53 +103,31 @@ private:
Ui::Dialog ui;
};
-// boiler plate code to connect incoming WebSockets to the WebChannel, such that they receive
-// messages and can access the published objects.
-class TransportHandler : public QObject
-{
- Q_OBJECT
-
-public:
- TransportHandler(QWebChannel *channel, QObject *parent = 0)
- : QObject(parent)
- , m_server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode)
- , m_channel(channel)
- {
- if (!m_server.listen(QHostAddress::LocalHost)) {
- qFatal("Failed to open web socket server.");
- }
-
- connect(&m_server, &QWebSocketServer::newConnection,
- this, &TransportHandler::handleNewConnection);
- }
-
- QString baseUrl() const
- {
- return m_server.serverUrl().toString();
- }
-
-private slots:
- void handleNewConnection()
- {
- m_channel->connectTo(new QWebChannelWebSocketTransport(m_server.nextPendingConnection()));
- }
-
-private:
- QWebSocketServer m_server;
- QWebChannel *m_channel;
-};
-
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QWebChannel channel;
- TransportHandler transportHandler(&channel);
+ QWebSocketServer server(QStringLiteral("QWebChannel Standalone Example Server"), QWebSocketServer::NonSecureMode);
+ if (!server.listen(QHostAddress::LocalHost)) {
+ qFatal("Failed to open web socket server.");
+ return 1;
+ }
- Dialog dialog(transportHandler.baseUrl());
+ WebSocketClientWrapper clientWrapper(&server);
+ QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected,
+ &channel, &QWebChannel::connectTo);
+
+ Dialog dialog;
channel.registerObject(QStringLiteral("dialog"), &dialog);
+ QUrl url = QUrl::fromLocalFile(SOURCE_DIR "/index.html");
+ url.setQuery(QStringLiteral("webChannelBaseUrl=") + server.serverUrl().toString());
+ QDesktopServices::openUrl(url);
+
+ dialog.displayMessage(QObject::tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString()));
+
return app.exec();
}