Document WebSocket::sendTextMessage(string message)
[contrib/qtwebsockets.git] / src / imports / qmlwebsockets / qqmlwebsocket.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Kurt Pattyn <pattyn.kurt@gmail.com>.
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSockets module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 /*!
43     \qmltype WebSocket
44     \instantiates QQmlWebSocket
45
46     \inqmlmodule Qt.WebSockets
47     \ingroup websockets-qml
48     \brief QML interface to QWebSocket.
49
50     WebSockets is a web technology providing full-duplex communications channels over a
51     single TCP connection.
52     The WebSocket protocol was standardized by the IETF as
53     \l {http://tools.ietf.org/html/rfc6455} {RFC 6455} in 2011.
54 */
55
56 /*!
57   \qmlproperty QUrl WebSocket::url
58   Server url to connect to. The url must have one of 2 schemes: \e ws:// or \e wss://.
59   When not supplied, then \e ws:// is used.
60   */
61
62 /*!
63   \qmlproperty Status WebSocket::status
64   Status of the WebSocket.
65
66   The status can have the following values:
67   \list
68   \li WebSockets.Connecting
69   \li WebSockets.Open
70   \li WebSockets.Closing
71   \li WebSockets.Closed
72   \li WebSockets.Error
73   \endlist
74   */
75
76 /*!
77   \qmlproperty QString WebSocket::errorString
78   Contains a description of the last error that occurred. When no error occurrred,
79   this string is empty.
80   */
81
82 /*!
83   \qmlproperty bool WebSocket::active
84   When set to true, a connection is made to the server with the given url.
85   When set to false, the connection is closed.
86   The default value is false.
87   */
88
89 /*!
90   \qmlsignal WebSocket::textMessageReceived(QString message)
91   This signal is emitted when a text message is received.
92   */
93
94 /*!
95   \qmlsignal WebSocket::statusChanged(Status status)
96   This signal is emitted when the status of the WebSocket changes.
97   the \l {WebSocket::status}{status} argument provides the current status.
98
99   \sa WebSocket::status
100   */
101
102 /*!
103   \qmlmethod void WebSocket::sendTextMessage(string message)
104   Sends \c message to the server.
105   */
106
107 #include "qqmlwebsocket.h"
108 #include <QtWebSockets/QWebSocket>
109
110 QT_BEGIN_NAMESPACE
111
112 QQmlWebSocket::QQmlWebSocket(QObject *parent) :
113     QObject(parent),
114     m_webSocket(),
115     m_status(Closed),
116     m_url(),
117     m_isActive(false),
118     m_componentCompleted(true),
119     m_errorString()
120 {
121 }
122
123 QQmlWebSocket::~QQmlWebSocket()
124 {
125 }
126
127 qint64 QQmlWebSocket::sendTextMessage(const QString &message)
128 {
129     if (m_status != Open) {
130         setErrorString(tr("Messages can only be sent when the socket is open."));
131         setStatus(Error);
132         return 0;
133     }
134     return m_webSocket->sendTextMessage(message);
135 }
136
137 QUrl QQmlWebSocket::url() const
138 {
139     return m_url;
140 }
141
142 void QQmlWebSocket::setUrl(const QUrl &url)
143 {
144     if (m_url == url) {
145         return;
146     }
147     if (m_webSocket && (m_status == Open)) {
148         m_webSocket->close();
149     }
150     m_url = url;
151     Q_EMIT urlChanged();
152     if (m_webSocket) {
153         m_webSocket->open(m_url);
154     }
155 }
156
157 QQmlWebSocket::Status QQmlWebSocket::status() const
158 {
159     return m_status;
160 }
161
162 QString QQmlWebSocket::errorString() const
163 {
164     return m_errorString;
165 }
166
167 void QQmlWebSocket::classBegin()
168 {
169     m_componentCompleted = false;
170     m_errorString = tr("QQmlWebSocket is not ready.");
171     m_status = Closed;
172 }
173
174 void QQmlWebSocket::componentComplete()
175 {
176     m_webSocket.reset(new (std::nothrow) QWebSocket());
177     if (Q_LIKELY(m_webSocket)) {
178         connect(m_webSocket.data(), &QWebSocket::textMessageReceived,
179                 this, &QQmlWebSocket::textMessageReceived);
180         typedef void (QWebSocket::* ErrorSignal)(QAbstractSocket::SocketError);
181         connect(m_webSocket.data(), static_cast<ErrorSignal>(&QWebSocket::error),
182                 this, &QQmlWebSocket::onError);
183         connect(m_webSocket.data(), &QWebSocket::stateChanged,
184                 this, &QQmlWebSocket::onStateChanged);
185
186         m_componentCompleted = true;
187
188         open();
189     }
190 }
191
192 void QQmlWebSocket::onError(QAbstractSocket::SocketError error)
193 {
194     Q_UNUSED(error)
195     setErrorString(m_webSocket->errorString());
196     setStatus(Error);
197 }
198
199 void QQmlWebSocket::onStateChanged(QAbstractSocket::SocketState state)
200 {
201     switch (state)
202     {
203         case QAbstractSocket::ConnectingState:
204         case QAbstractSocket::BoundState:
205         case QAbstractSocket::HostLookupState:
206         {
207             setStatus(Connecting);
208             break;
209         }
210         case QAbstractSocket::UnconnectedState:
211         {
212             setStatus(Closed);
213             break;
214         }
215         case QAbstractSocket::ConnectedState:
216         {
217             setStatus(Open);
218             break;
219         }
220         case QAbstractSocket::ClosingState:
221         {
222             setStatus(Closing);
223             break;
224         }
225         default:
226         {
227             setStatus(Connecting);
228             break;
229         }
230     }
231 }
232
233 void QQmlWebSocket::setStatus(QQmlWebSocket::Status status)
234 {
235     if (m_status == status) {
236         return;
237     }
238     m_status = status;
239     if (status != Error) {
240         setErrorString();
241     }
242     Q_EMIT statusChanged(m_status);
243 }
244
245 void QQmlWebSocket::setActive(bool active)
246 {
247     if (m_isActive == active) {
248         return;
249     }
250     m_isActive = active;
251     Q_EMIT activeChanged(m_isActive);
252     if (!m_componentCompleted) {
253         return;
254     }
255     if (m_isActive) {
256         open();
257     } else {
258         close();
259     }
260 }
261
262 bool QQmlWebSocket::isActive() const
263 {
264     return m_isActive;
265 }
266
267 void QQmlWebSocket::open()
268 {
269     if (m_componentCompleted && m_isActive && m_url.isValid() && Q_LIKELY(m_webSocket)) {
270         m_webSocket->open(m_url);
271     }
272 }
273
274 void QQmlWebSocket::close()
275 {
276     if (m_componentCompleted && Q_LIKELY(m_webSocket)) {
277         m_webSocket->close();
278     }
279 }
280
281 void QQmlWebSocket::setErrorString(QString errorString)
282 {
283     if (m_errorString == errorString) {
284         return;
285     }
286     m_errorString = errorString;
287     Q_EMIT errorStringChanged(m_errorString);
288 }
289
290 QT_END_NAMESPACE