2 import QtQuick.Window 2.0
3 import QtQuick.Controls 1.4
4 import QtWebSockets 1.0
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: ""
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 !")
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") {
41 connect_button.enabled = true
42 refresh_button.enabled = false
43 logout_button.enabled = false
44 websocket.active = false // close the socket
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"
57 console.log (status_str)
63 anchors.left: parent.left
64 anchors.top: parent.top
65 anchors.horizontalCenter: parent.horizontalCenter
69 text: "QML Websocket AFB Sample"
72 anchors.centerIn: parent
78 text: "URL: " + websocket.url
83 text: "Verb: " + verb_str
88 text: "Token: " + token_str
97 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", null ]';
98 if (!websocket.active)
99 websocket.active = true
101 websocket.sendTextMessage (request_str)
110 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '",,"' + token_str +'" ]';
111 websocket.sendTextMessage (request_str)
121 request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", ]';
122 websocket.sendTextMessage (request_str)
130 text: "Request: " + request_str
136 text: "Status: " + status_str