Update license headers and add new license files
[contrib/qtwebsockets.git] / examples / websockets / qmlwebsocketserver / qml / qmlwebsocketserver / main.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtWebSocket module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights. These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33
34 import QtQuick 2.0
35 import Qt.WebSockets 1.0
36
37 Rectangle {
38     width: 360
39     height: 360
40
41     function appendMessage(message) {
42         messageBox.text += "\n" + message
43     }
44
45     WebSocketServer {
46         id: server
47         listen: true
48         onClientConnected: {
49             webSocket.onTextMessageReceived.connect(function(message) {
50                 appendMessage(qsTr("Server received message: %1").arg(message));
51                 webSocket.sendTextMessage(qsTr("Hello Client!"));
52             });
53         }
54         onErrorStringChanged: {
55             appendMessage(qsTr("Server error: %1").arg(errorString));
56         }
57     }
58
59     WebSocket {
60         id: socket
61         url: server.url
62         onTextMessageReceived: appendMessage(qsTr("Client received message: %1").arg(message))
63         onStatusChanged: {
64             if (socket.status == WebSocket.Error) {
65                 appendMessage(qsTr("Client error: %1").arg(socket.errorString));
66             } else if (socket.status == WebSocket.Closed) {
67                 appendMessage(qsTr("Client socket closed."));
68             }
69         }
70     }
71
72     Text {
73         id: messageBox
74         text: qsTr("Click to send a message!")
75         anchors.fill: parent
76
77         MouseArea {
78             anchors.fill: parent
79             onClicked: {
80                 socket.sendTextMessage(qsTr("Hello Server!"));
81             }
82         }
83     }
84 }