summaryrefslogtreecommitdiff
path: root/src/imports
diff options
context:
space:
mode:
authorKurt Pattyn <pattyn.kurt@gmail.com>2013-12-22 18:24:21 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-12-23 19:26:09 +0100
commit23ccec8ce246634799b06f55b85478b3fbbb356d (patch)
tree7144ceb793362636931c4d26f9cc5f1561672fda /src/imports
parent19d8d19af44b9075f745790380c3fe663d8e7fd0 (diff)
downloadqtwebsockets-23ccec8ce246634799b06f55b85478b3fbbb356d.tar.gz
Add optimizations
Change-Id: Icd293f832e2d7a6272d4953c1994065d16222375 Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
Diffstat (limited to 'src/imports')
-rw-r--r--src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp4
-rw-r--r--src/imports/qmlwebsockets/qqmlwebsocket.cpp28
-rw-r--r--src/imports/qmlwebsockets/qqmlwebsocket.h7
3 files changed, 26 insertions, 13 deletions
diff --git a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp
index c8d2cd6..6664e59 100644
--- a/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp
+++ b/src/imports/qmlwebsockets/qmlwebsockets_plugin.cpp
@@ -43,6 +43,8 @@
#include <QtQml>
+QT_BEGIN_NAMESPACE
+
void QmlWebsocket_plugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("Qt.WebSockets"));
@@ -50,3 +52,5 @@ void QmlWebsocket_plugin::registerTypes(const char *uri)
// @uri Qt.WebSockets
qmlRegisterType<QQmlWebSocket>(uri, 1 /*major*/, 0 /*minor*/, "WebSocket");
}
+
+QT_END_NAMESPACE
diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.cpp b/src/imports/qmlwebsockets/qqmlwebsocket.cpp
index 9d2ce7f..3483d75 100644
--- a/src/imports/qmlwebsockets/qqmlwebsocket.cpp
+++ b/src/imports/qmlwebsockets/qqmlwebsocket.cpp
@@ -59,6 +59,8 @@
#include "qqmlwebsocket.h"
#include <QtWebSockets/QWebSocket>
+QT_BEGIN_NAMESPACE
+
QQmlWebSocket::QQmlWebSocket(QObject *parent) :
QObject(parent),
m_webSocket(),
@@ -74,14 +76,14 @@ QQmlWebSocket::~QQmlWebSocket()
{
}
-void QQmlWebSocket::sendTextMessage(const QString &message)
+qint64 QQmlWebSocket::sendTextMessage(const QString &message)
{
if (m_status != Open) {
setErrorString(tr("Messages can only be send when the socket has Open status."));
setStatus(Error);
- return;
+ return 0;
}
- m_webSocket->write(message);
+ return m_webSocket->write(message);
}
QUrl QQmlWebSocket::url() const
@@ -123,14 +125,16 @@ void QQmlWebSocket::classBegin()
void QQmlWebSocket::componentComplete()
{
- m_webSocket.reset(new QWebSocket());
- connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString)));
- connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
- connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
+ m_webSocket.reset(new (std::nothrow) QWebSocket());
+ if (Q_LIKELY(m_webSocket)) {
+ connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString)));
+ connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
+ connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
- m_componentCompleted = true;
+ m_componentCompleted = true;
- open();
+ open();
+ }
}
void QQmlWebSocket::onError(QAbstractSocket::SocketError error)
@@ -210,14 +214,14 @@ bool QQmlWebSocket::isActive() const
void QQmlWebSocket::open()
{
- if (m_componentCompleted && m_isActive && m_url.isValid() && m_webSocket) {
+ if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) {
m_webSocket->open(m_url);
}
}
void QQmlWebSocket::close()
{
- if (m_componentCompleted && m_webSocket) {
+ if (m_componentCompleted && Q_LIKELY(m_webSocket)) {
m_webSocket->close();
}
}
@@ -230,3 +234,5 @@ void QQmlWebSocket::setErrorString(QString errorString)
m_errorString = errorString;
Q_EMIT errorStringChanged(m_errorString);
}
+
+QT_END_NAMESPACE
diff --git a/src/imports/qmlwebsockets/qqmlwebsocket.h b/src/imports/qmlwebsockets/qqmlwebsocket.h
index 47cf6fa..b9d7769 100644
--- a/src/imports/qmlwebsockets/qqmlwebsocket.h
+++ b/src/imports/qmlwebsockets/qqmlwebsocket.h
@@ -48,6 +48,8 @@
#include <QScopedPointer>
#include <QWebSocket>
+QT_BEGIN_NAMESPACE
+
class QQmlWebSocket : public QObject, public QQmlParserStatus
{
Q_OBJECT
@@ -81,8 +83,7 @@ public:
void setActive(bool active);
bool isActive() const;
-public Q_SLOTS:
- void sendTextMessage(const QString &message);
+ Q_INVOKABLE qint64 sendTextMessage(const QString &message);
Q_SIGNALS:
@@ -114,4 +115,6 @@ private:
void setErrorString(QString errorString = QString());
};
+QT_END_NAMESPACE
+
#endif // QQMLWEBSOCKET_H