summaryrefslogtreecommitdiffstats
path: root/test/token-websock.qml
diff options
context:
space:
mode:
authorManuel Bachmann <manuel.bachmann@iot.bzh>2016-05-18 21:15:31 +0200
committerManuel Bachmann <manuel.bachmann@iot.bzh>2016-05-18 21:15:31 +0200
commitc044f0961a1c276a13791e7559027505c5292073 (patch)
treee0b33acd75ce7a27c76d9f00bfe2de6db17bbec0 /test/token-websock.qml
parent779f425daa66d2b0cd431fdc6f7f4ed976cbae15 (diff)
Add QML WebSockets sample (token API)
Creates, refreshes and destroys tokens via websockets. Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
Diffstat (limited to 'test/token-websock.qml')
-rw-r--r--test/token-websock.qml140
1 files changed, 140 insertions, 0 deletions
diff --git a/test/token-websock.qml b/test/token-websock.qml
new file mode 100644
index 00000000..d8665c71
--- /dev/null
+++ b/test/token-websock.qml
@@ -0,0 +1,140 @@
+import QtQuick 2.0
+import QtQuick.Window 2.0
+import QtQuick.Controls 1.4
+import QtWebSockets 1.0
+
+Window {
+
+ property string address_str: "ws://localhost:1234/api?token=123456"
+ property string token_str: ""
+ property string api_str: "token"
+ property string verb_str: ""
+ property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 }
+ property string request_str: ""
+ property string status_str: ""
+
+ visible: true
+ width: 320
+ height: 300
+
+ WebSocket {
+ id: websocket
+ url: address_str
+ onTextMessageReceived: {
+ var message_json = JSON.parse (message);
+ console.log ("Raw response: " + message)
+ console.log ("JSON response: " + message_json)
+ /* server is not happy with our request, ignore it */
+ if (message_json[0] != msgid_enu.retok) {
+ console.log ("Return value is not ok !")
+ return
+ }
+ /* token creation or refresh happened, store it and enable buttons */
+ if ((verb_str == "create") || (verb_str == "refresh")) {
+ token_str = message_json[3]
+ refresh_button.enabled = true
+ reset_button.enabled = true
+ /* token reset happened, remove it and disable buttons */
+ } else if (verb_str == "reset") {
+ token_str = ""
+ refresh_button.enabled = false
+ reset_button.enabled = false
+ websocket.active = false // close the socket
+ }
+ }
+ onStatusChanged: {
+ if (websocket.status == WebSocket.Error) {
+ status_str = "Error: " + websocket.errorString
+ } else if (websocket.status == WebSocket.Open) {
+ status_str = "Socket opened; sending message..."
+ if (verb_str == "create")
+ websocket.sendTextMessage (request_str)
+ } else if (websocket.status == WebSocket.Closed) {
+ status_str = "Socket closed"
+ }
+ console.log (status_str)
+ }
+ active: false
+ }
+
+ Rectangle {
+ anchors.left: parent.left
+ anchors.top: parent.top
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.margins: 20
+
+ Label {
+ text: "QML Websocket AFB Sample"
+ font.pixelSize: 18
+ font.bold: true
+ anchors.centerIn: parent
+ y: 0
+ }
+
+ Text {
+ id: url_notifier
+ text: "URL: " + websocket.url
+ y: 20
+ }
+ Text {
+ id: verb_notifier
+ text: "Verb: " + verb_str
+ y: 40
+ }
+ Text {
+ id: token_notifier
+ text: "Token: " + token_str
+ y: 60
+ }
+
+ Button {
+ id: create_button
+ text: "Create token"
+ onClicked: {
+ verb_str = "create"
+ request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", ]';
+ if (!websocket.active)
+ websocket.active = true
+ else
+ websocket.sendTextMessage (request_str)
+ }
+ y: 80
+ }
+ Button {
+ id: refresh_button
+ text: "Refresh token"
+ onClicked: {
+ verb_str = "refresh"
+ request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '",,"' + token_str +'" ]';
+ websocket.sendTextMessage (request_str)
+ }
+ y: 110
+ enabled: false
+ }
+ Button {
+ id: reset_button
+ text: "Reset token"
+ onClicked: {
+ verb_str = "reset"
+ request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", ]';
+ websocket.sendTextMessage (request_str)
+ }
+ y: 140
+ enabled: false
+ }
+
+ Text {
+ id: request_notifier
+ text: "Request: " + request_str
+ y: 170
+ }
+
+ Text {
+ id: status_notifier
+ text: "Status: " + status_str
+ y: 190
+ }
+
+ }
+
+}