summaryrefslogtreecommitdiffstats
path: root/examples/hybrid-qml/xxxxxx-hybrid-qml-app.qml
blob: eded2d04100bda246e106193d0ad9aeadde05f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
import QtWebSockets 1.0

Window {
	 // VARIABLES

	property string port_str: Qt.application.arguments[1]
	property string token_str: Qt.application.arguments[2]
	property string address_str: "ws://localhost:"+port_str+"/api?token="+token_str
	property string request_str: ""
	property string status_str: "waiting..."
	property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 }

	 // WINDOW PROPERTIES

	visible: true
	width: 340
	height: 160

	 // WEBSOCKET WIDGET (MAIN LOGIC)

	WebSocket {
		id: websocket
		url: address_str
		onTextMessageReceived: {
			 // VERB RESPONSE VALIDATION
			var message_json = JSON.parse (message)
			var request_json = message_json[2].request
			if (message_json[0] != msgid_enu.retok) {
				console.log ("Return value is not ok !")
				status_str = "Bad return value, binding probably not installed"
				return
			}
			 // VERB RESPONSE PARSING AND LOGIC
			status_str = request_json.info
		}
		onStatusChanged: {
			if (websocket.status == WebSocket.Error)
				status_str = "WebSocket error: " + websocket.errorString
		}
		active: true
	}

	 // OTHER WIDGETS

	Rectangle {
		anchors.left: parent.left
		anchors.top: parent.top
		anchors.horizontalCenter: parent.horizontalCenter
		anchors.margins: 20

		 // TITLE SECTION
		Label {
			text: "QML Websocket Sample Application"
			font.pixelSize: 18
			font.bold: true
			anchors.centerIn: parent
			y: 0
		}
		Text {
			id: url_notifier
			text: "<b>URL:</b> " + websocket.url
			y: 20
		}

		 // PING BUTTON
		Button {
			text: "Ping!"
			onClicked: {
				request_str = '[' + msgid_enu.call + ',"99999","xxxxxx/ping", null ]';
				websocket.sendTextMessage (request_str)
			}
			anchors.horizontalCenter: parent.horizontalCenter
			y: 60
		}

		 // STATUS SECTION
		Text {
			id: status_notifier
			text: "<b>Status</b>: " + status_str
			y: 100
		}
	}

}