From 9b804c270d11d34d37c08a5145dba083a6b0c1a7 Mon Sep 17 00:00:00 2001 From: Nobuaki Sukegawa Date: Tue, 15 Sep 2015 06:21:47 +0900 Subject: Add binary message support to QML WebSocket type Change-Id: I4472e899606d261420141e7b382717cbe12943c8 Reviewed-by: Simon Hausmann --- src/imports/qmlwebsockets/plugins.qmltypes | 17 ++++++++++++--- src/imports/qmlwebsockets/qmlwebsockets.pro | 2 +- src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp | 1 + src/imports/qmlwebsockets/qqmlwebsocket.cpp | 24 ++++++++++++++++++++++ src/imports/qmlwebsockets/qqmlwebsocket.h | 3 ++- 5 files changed, 42 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/imports/qmlwebsockets/plugins.qmltypes b/src/imports/qmlwebsockets/plugins.qmltypes index b79da44..cbbb628 100644 --- a/src/imports/qmlwebsockets/plugins.qmltypes +++ b/src/imports/qmlwebsockets/plugins.qmltypes @@ -4,15 +4,15 @@ import QtQuick.tooling 1.2 // It is used for QML tooling purposes only. // // This file was auto-generated by: -// 'qmlplugindump -nonrelocatable QtWebSockets 1.0' +// 'qmlplugindump -nonrelocatable QtWebSockets 1.1' Module { dependencies: [] Component { name: "QQmlWebSocket" prototype: "QObject" - exports: ["QtWebSockets/WebSocket 1.0"] - exportMetaObjectRevisions: [0] + exports: ["QtWebSockets/WebSocket 1.0", "QtWebSockets/WebSocket 1.1"] + exportMetaObjectRevisions: [0, 1] Enum { name: "Status" values: { @@ -31,6 +31,11 @@ Module { name: "textMessageReceived" Parameter { name: "message"; type: "string" } } + Signal { + name: "binaryMessageReceived" + revision: 1 + Parameter { name: "message"; type: "QByteArray" } + } Signal { name: "statusChanged" Parameter { name: "status"; type: "Status" } @@ -48,6 +53,12 @@ Module { type: "qlonglong" Parameter { name: "message"; type: "string" } } + Method { + name: "sendBinaryMessage" + revision: 1 + type: "qlonglong" + Parameter { name: "message"; type: "QByteArray" } + } } Component { name: "QQmlWebSocketServer" diff --git a/src/imports/qmlwebsockets/qmlwebsockets.pro b/src/imports/qmlwebsockets/qmlwebsockets.pro index dea84a8..fef47f4 100644 --- a/src/imports/qmlwebsockets/qmlwebsockets.pro +++ b/src/imports/qmlwebsockets/qmlwebsockets.pro @@ -1,4 +1,4 @@ -QT = core websockets qml +QT = core websockets qml qml-private core-private TARGETPATH = QtWebSockets diff --git a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp index 11c9dec..24fe34a 100644 --- a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp +++ b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp @@ -53,6 +53,7 @@ void QtWebSocketsDeclarativeModule::registerTypes(const char *uri) // @uri QtWebSockets qmlRegisterType(uri, 1 /*major*/, 0 /*minor*/, "WebSocket"); + qmlRegisterType(uri, 1 /*major*/, 1 /*minor*/, "WebSocket"); qmlRegisterType(uri, 1 /*major*/, 0 /*minor*/, "WebSocketServer"); } diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.cpp b/src/imports/qmlwebsockets/qqmlwebsocket.cpp index a5c7aeb..13b986e 100644 --- a/src/imports/qmlwebsockets/qqmlwebsocket.cpp +++ b/src/imports/qmlwebsockets/qqmlwebsocket.cpp @@ -90,6 +90,12 @@ This signal is emitted when a text message is received. */ +/*! + \qmlsignal WebSocket::binaryMessageReceived(QString message) + \since 5.8 + This signal is emitted when a binary message is received. + */ + /*! \qmlsignal WebSocket::statusChanged(Status status) This signal is emitted when the status of the WebSocket changes. @@ -103,6 +109,12 @@ Sends \c message to the server. */ +/*! + \qmlmethod void WebSocket::sendBinaryMessage(ArrayBuffer message) + \since 5.8 + Sends \c message to the server. + */ + #include "qqmlwebsocket.h" #include @@ -145,6 +157,16 @@ qint64 QQmlWebSocket::sendTextMessage(const QString &message) return m_webSocket->sendTextMessage(message); } +qint64 QQmlWebSocket::sendBinaryMessage(const QByteArray &message) +{ + if (m_status != Open) { + setErrorString(tr("Messages can only be sent when the socket is open.")); + setStatus(Error); + return 0; + } + return m_webSocket->sendBinaryMessage(message); +} + QUrl QQmlWebSocket::url() const { return m_url; @@ -199,6 +221,8 @@ void QQmlWebSocket::setSocket(QWebSocket *socket) m_webSocket->setParent(Q_NULLPTR); connect(m_webSocket.data(), &QWebSocket::textMessageReceived, this, &QQmlWebSocket::textMessageReceived); + connect(m_webSocket.data(), &QWebSocket::binaryMessageReceived, + this, &QQmlWebSocket::binaryMessageReceived); typedef void (QWebSocket::* ErrorSignal)(QAbstractSocket::SocketError); connect(m_webSocket.data(), static_cast(&QWebSocket::error), this, &QQmlWebSocket::onError); diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.h b/src/imports/qmlwebsockets/qqmlwebsocket.h index c5682ed..c3a808f 100644 --- a/src/imports/qmlwebsockets/qqmlwebsocket.h +++ b/src/imports/qmlwebsockets/qqmlwebsocket.h @@ -83,10 +83,11 @@ public: bool isActive() const; Q_INVOKABLE qint64 sendTextMessage(const QString &message); - + Q_REVISION(1) Q_INVOKABLE qint64 sendBinaryMessage(const QByteArray &message); Q_SIGNALS: void textMessageReceived(QString message); + Q_REVISION(1) void binaryMessageReceived(QByteArray message); void statusChanged(Status status); void activeChanged(bool isActive); void errorStringChanged(QString errorString); -- cgit v1.2.1