summaryrefslogtreecommitdiffstats
path: root/LICENSE.MIT
blob: a6919eb7e1fdbda5d53fd9d8e0f7114f2274457b (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
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Note:
Individual files contain the following tag instead of the full license text.

    SPDX-License-Identifier: MIT

This enables machine processing of license information based on the SPDX
License Identifiers that are here available: http://spdx.org/licenses/
} /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
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: "auth"
	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 == "connect") || (verb_str == "refresh")) {
				token_str = message_json[3]
				connect_button.enabled = false
				refresh_button.enabled = true
				logout_button.enabled = true
			 /* token reset happened, remove it and disable buttons */
			} else if (verb_str == "logout") {
				token_str = ""
				connect_button.enabled = true
				refresh_button.enabled = false
				logout_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 == "connect")
					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: connect_button
			text: "Connect"
			onClicked: {
				verb_str = "connect"
				request_str = '[' + msgid_enu.call + ',"99999","' + api_str+'/'+verb_str + '", null ]';
				if (!websocket.active)
					websocket.active = true
				else
					websocket.sendTextMessage (request_str)
			}
			y: 80
		}
		Button {
			id: refresh_button
			text: "Refresh"
			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: logout_button
			text: "Logout"
			onClicked: {
				verb_str = "logout"
				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
		}

	}

}