1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
44 \instantiates QQmlWebSocket
46 \inqmlmodule Qt.WebSockets
47 \brief QML interface to QWebSocket.
49 WebSockets is a web technology providing full-duplex communications channels over a single TCP connection.
50 The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011 (see http://tools.ietf.org/html/rfc6455).
54 \qmlproperty QUrl WebSocket::url
55 Server url to connect to. The url must have one of 2 schemes: {ws://} or {wss://}.
56 When not supplied, then {ws://} is used.
60 \qmlproperty Status WebSocket::status
61 Status of the WebSocket.
63 The status can have the following values:
65 \li WebSockets.Connecting
67 \li WebSockets.Closing
74 \qmlproperty QString WebSocket::errorString
75 Contains a description of the last error that occurred. When no error occurrred,
80 \qmlproperty bool WebSocket::active
81 When set to true, a connection is made to the server with the given url.
82 When set to false, the connection is closed.
83 The default value is false.
87 \qmlsignal WebSocket::textMessageReceived(QString message)
88 This signal is emitted when a text message is received.
92 \qmlsignal WebSocket::statusChanged(Status status)
93 This signal is emitted when the status of the WebSocket changes.
94 the \l {WebSocket::status}{status} argument provides the current status.
99 #include "qqmlwebsocket.h"
100 #include <QtWebSockets/QWebSocket>
104 QQmlWebSocket::QQmlWebSocket(QObject *parent) :
110 m_componentCompleted(true),
115 QQmlWebSocket::~QQmlWebSocket()
119 qint64 QQmlWebSocket::sendTextMessage(const QString &message)
121 if (m_status != Open) {
122 setErrorString(tr("Messages can only be send when the socket has Open status."));
126 return m_webSocket->write(message);
129 QUrl QQmlWebSocket::url() const
134 void QQmlWebSocket::setUrl(const QUrl &url)
139 if (m_webSocket && (m_status == Open)) {
140 m_webSocket->close();
145 m_webSocket->open(m_url);
149 QQmlWebSocket::Status QQmlWebSocket::status() const
154 QString QQmlWebSocket::errorString() const
156 return m_errorString;
159 void QQmlWebSocket::classBegin()
161 m_componentCompleted = false;
162 m_errorString = tr("QQmlWebSocket is not ready.");
166 void QQmlWebSocket::componentComplete()
168 m_webSocket.reset(new (std::nothrow) QWebSocket());
169 if (Q_LIKELY(m_webSocket)) {
170 connect(m_webSocket.data(), SIGNAL(textMessageReceived(QString)), this, SIGNAL(textMessageReceived(QString)));
171 connect(m_webSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
172 connect(m_webSocket.data(), SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
174 m_componentCompleted = true;
180 void QQmlWebSocket::onError(QAbstractSocket::SocketError error)
183 setErrorString(m_webSocket->errorString());
187 void QQmlWebSocket::onStateChanged(QAbstractSocket::SocketState state)
191 case QAbstractSocket::ConnectingState:
192 case QAbstractSocket::BoundState:
193 case QAbstractSocket::HostLookupState:
195 setStatus(Connecting);
198 case QAbstractSocket::UnconnectedState:
203 case QAbstractSocket::ConnectedState:
208 case QAbstractSocket::ClosingState:
215 setStatus(Connecting);
221 void QQmlWebSocket::setStatus(QQmlWebSocket::Status status)
223 if (m_status == status) {
227 if (status != Error) {
230 Q_EMIT statusChanged(m_status);
233 void QQmlWebSocket::setActive(bool active)
235 if (m_isActive == active) {
239 Q_EMIT activeChanged(m_isActive);
240 if (!m_componentCompleted) {
250 bool QQmlWebSocket::isActive() const
255 void QQmlWebSocket::open()
257 if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) {
258 m_webSocket->open(m_url);
262 void QQmlWebSocket::close()
264 if (m_componentCompleted && Q_LIKELY(m_webSocket)) {
265 m_webSocket->close();
269 void QQmlWebSocket::setErrorString(QString errorString)
271 if (m_errorString == errorString) {
274 m_errorString = errorString;
275 Q_EMIT errorStringChanged(m_errorString);