e11c3d52fca77acf0faa67533ddf9f08bbebf1b8
[src/app-framework-binder.git] / test / token-websock.qml
1 import QtQuick 2.0
2 import QtQuick.Window 2.0
3 import QtQuick.Controls 1.4
4 import QtWebSockets 1.0
5
6 Window {
7
8         property string address_str: "ws://localhost:1234/api?token=123456"
9         property string token_str: ""
10         property string api_str: "auth"
11         property string verb_str: ""
12         property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 }
13         property string request_str: ""
14         property string status_str: ""
15
16         visible: true
17         width: 320
18         height: 300
19
20         WebSocket {
21                 id: websocket
22                 url: address_str
23                 onTextMessageReceived: {
24                         var message_json = JSON.parse (message);
25                         console.log ("Raw response: " + message)
26                         console.log ("JSON response: " + message_json)
27                          /* server is not happy with our request, ignore it */
28                         if (message_json[0] != msgid_enu.retok) {
29                                 console.log ("Return value is not ok !")
30                                 return
31                         }
32                          /* token creation or refresh happened, store it and enable buttons */
33                         if ((verb_str == "connect") || (verb_str == "refresh")) {
34                                 token_str = message_json[3]
35                                 connect_button.enabled = false
36                                 refresh_button.enabled = true
37                                 logout_button.enabled = true
38                          /* token reset happened, remove it and disable buttons */
39                         } else if (verb_str == "logout") {
40                                 token_str = ""
41                                 connect_button.enabled = true
42                                 refresh_button.enabled = false
43                                 logout_button.enabled = false
44                                 websocket.active = false        // close the socket
45                         }
46                 }
47                 onStatusChanged: {
48                         if (websocket.status == WebSocket.Error) {
49                                 status_str = "Error: " + websocket.errorString
50                         } else if (websocket.status == WebSocket.Open) {
51                                 status_str = "Socket opened; sending message..."
52                                 if (verb_str == "connect")
53                                         websocket.sendTextMessage (request_str)
54                                 } else if (websocket.status == WebSocket.Closed) {
55                                         status_str = "Socket closed"
56                                 }
57                                 console.log (status_str)
58                 }
59                 active: false
60         }
61
62         Rectangle {
63                 anchors.left: parent.left
64                 anchors.top: parent.top
65                 anchors.horizontalCenter: parent.horizontalCenter
66                 anchors.margins: 20
67
68                 Label {
69                         text: "QML Websocket AFB Sample"
70                         font.pixelSize: 18
71                         font.bold: true
72                         anchors.centerIn: parent
73                         y: 0
74                 }
75
76                 Text {
77                         id: url_notifier
78                         text: "URL: " + websocket.url
79                         y: 20
80                 }
81                 Text {
82                         id: verb_notifier
83                         text: "Verb: " + verb_str
84                         y: 40
85                 }
86                 Text {
87                         id: token_notifier
88                         text: "Token: " + token_str
89                         y: 60
90                 }
91
92                 Button {
93                         id: connect_button
94                         text: "Connect"
95                         onClicked: {
96                                 verb_str = "connect"
97                                 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", null ]';
98                                 if (!websocket.active)
99                                         websocket.active = true
100                                 else
101                                         websocket.sendTextMessage (request_str)
102                         }
103                         y: 80
104                 }
105                 Button {
106                         id: refresh_button
107                         text: "Refresh"
108                         onClicked: {
109                                 verb_str = "refresh"
110                                 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '",,"' + token_str +'" ]';
111                                 websocket.sendTextMessage (request_str)
112                         }
113                         y: 110
114                         enabled: false
115                 }
116                 Button {
117                         id: logout_button
118                         text: "Logout"
119                         onClicked: {
120                                 verb_str = "logout"
121                                 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", ]';
122                                 websocket.sendTextMessage (request_str)
123                         }
124                         y: 140
125                         enabled: false
126                 } 
127
128                 Text {
129                         id: request_notifier
130                         text: "Request: " + request_str
131                         y: 170
132                 }
133
134                 Text {
135                         id: status_notifier
136                         text: "Status: " + status_str
137                         y: 190
138                 }
139
140         }
141
142 }

©2017 Automotive Grade Linux, a Linux Foundation Collaborative Project. All Rights Reserved. Linux Foundation is a registered trademark of The Linux Foundation.
Linux is a registered trademark of Linus Torvalds.