summaryrefslogtreecommitdiff
path: root/src/webchannel/qwebchannelsocket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/webchannel/qwebchannelsocket.cpp')
-rw-r--r--src/webchannel/qwebchannelsocket.cpp61
1 files changed, 49 insertions, 12 deletions
diff --git a/src/webchannel/qwebchannelsocket.cpp b/src/webchannel/qwebchannelsocket.cpp
index b3daf8f..e6a58c8 100644
--- a/src/webchannel/qwebchannelsocket.cpp
+++ b/src/webchannel/qwebchannelsocket.cpp
@@ -44,18 +44,26 @@
#include <QUuid>
#include <QDebug>
+#include <QtWebSockets/QWebSocket>
+
QT_BEGIN_NAMESPACE
QWebChannelSocket::QWebChannelSocket(QObject *parent)
- : QWebSocketServer(parent)
+ : QWebSocketServer(QStringLiteral("QWebChannel Server"), NonSecureMode, parent)
, m_messageHandler(Q_NULLPTR)
, m_useSecret(true)
, m_starting(false)
{
- connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
+ connect(this, SIGNAL(acceptError(QAbstractSocket::SocketError)),
SLOT(socketError()));
- connect(this, SIGNAL(textDataReceived(QString)),
- SLOT(messageReceived(QString)));
+ connect(this, SIGNAL(newConnection()),
+ SLOT(validateNewConnection()));
+}
+
+QWebChannelSocket::~QWebChannelSocket()
+{
+ close();
+ qDeleteAll(m_clients);
}
void QWebChannelSocket::initLater()
@@ -66,13 +74,29 @@ void QWebChannelSocket::initLater()
m_starting = true;
}
-bool QWebChannelSocket::isValid(const HeaderData &connection)
+void QWebChannelSocket::sendMessage(const QString &message)
+{
+ foreach (QWebSocket *client, m_clients) {
+ client->sendTextMessage(message);
+ }
+}
+
+void QWebChannelSocket::validateNewConnection()
{
- if (!QWebSocketServer::isValid(connection)) {
- return false;
+ QWebSocket *client = nextPendingConnection();
+ // FIXME: client->protocol() != QStringLiteral("QWebChannel")
+ // protocols are not supported in QtWebSockets yet...
+ if (m_useSecret && client->requestUrl().path() != m_secret)
+ {
+ client->close(QWebSocketProtocol::CloseCodeBadOperation);
+ client->deleteLater();
+ } else {
+ connect(client, SIGNAL(textMessageReceived(QString)),
+ SLOT(messageReceived(QString)));
+ connect(client, SIGNAL(disconnected()),
+ SLOT(clientDisconnected()));
+ m_clients << client;
}
- return connection.protocol == QByteArrayLiteral("QWebChannel")
- && connection.path == m_secret;
}
void QWebChannelSocket::init()
@@ -81,9 +105,9 @@ void QWebChannelSocket::init()
m_starting = false;
if (m_useSecret) {
- m_secret = QUuid::createUuid().toByteArray();
+ m_secret = QUuid::createUuid().toString();
// replace { by /
- m_secret[0] = '/';
+ m_secret[0] = QLatin1Char('/');
// chop of trailing }
m_secret.chop(1);
}
@@ -93,7 +117,7 @@ void QWebChannelSocket::init()
return;
}
- m_baseUrl = QStringLiteral("127.0.0.1:%1%2").arg(port()).arg(QString::fromLatin1(m_secret));
+ m_baseUrl = QStringLiteral("127.0.0.1:%1%2").arg(serverPort()).arg(m_secret);
emit initialized();
emit baseUrlChanged(m_baseUrl);
}
@@ -108,6 +132,19 @@ void QWebChannelSocket::messageReceived(const QString &message)
if (m_messageHandler) {
m_messageHandler->handleMessage(message);
}
+ emit textDataReceived(message);
+}
+
+void QWebChannelSocket::clientDisconnected()
+{
+ QWebSocket *client = qobject_cast<QWebSocket*>(sender());
+ if (!client) {
+ return;
+ }
+ const int idx = m_clients.indexOf(client);
+ Q_ASSERT(idx != -1);
+ m_clients.remove(idx);
+ client->deleteLater();
}
QT_END_NAMESPACE