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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.block {
background-color: #ccc;
padding: 1em;
margin: 1em;
}
</style>
</head>
<body>
<h1>Key-Value Store Client</h1>
<noscript>
<span style="color: #f00; font-weight: bold;">
You need to turn on JavaScript.
</span>
</noscript>
<div class="block"><button onclick="getAll()">Get all</button> Retrieve all key-value pairs from store and log to console.</div>
<div class="block"><button onclick="deleteAll()">Delete all</button> Delete all data from store.</div>
<div class="block">
<form>
<p>Key: <input id="form_key" type="text" size="50" maxlength="50" value="key1"></p>
<p>Value 1: <input id="form_value1" type="text" size="50" maxlength="50" value="foobar"></p>
<p>Value 2: <input id="form_value2" type="text" size="50" maxlength="50" value="123"></p>
<p>Value 3: <input id="form_value3" type="checkbox" checked></p>
</form>
<p><button onclick="setKeyValue()">Set Value</button> Set key-value pair.</p>
<p><button onclick="deleteKey()">Delete Key</button> Delete key-value by key.</p>
<p><button onclick="getValue()">Get Key</button> Get key-value by key.</p>
</div>
<pre id="log" style="height: 20em; overflow-y: scroll; background-color: #faa;"></pre>
<script src="/autobahn.min.js"></script>
<script type="text/javascript">
var session = null;
// the URL of the WAMP Router (e.g. Crossbar.io)
//
var wsuri;
if (document.location.origin == "file://") {
wsuri = "ws://localhost:9000";
} else {
wsuri = "ws://" + document.location.hostname + ":9000";
}
// connect to WAMP server
//
var connection = new autobahn.Connection({
url: wsuri,
realm: 'realm1'
});
connection.onopen = function (new_session) {
console.log("connected to " + wsuri);
session = new_session;
};
connection.onclose = function (reason, details) {
console.log("connection gone", reason, details);
new_session = null;
}
connection.open();
function setKeyValue() {
var key = document.getElementById('form_key').value;
var value = {
value1: document.getElementById('form_value1').value,
value2: document.getElementById('form_value2').value,
value3: document.getElementById('form_value3').checked,
modified: new Date()
}
session.call("com.example.keyvalue.set", [key, value]).then(
function () {
console.log("Value stored!");
}
);
}
function getValue() {
var key = document.getElementById('form_key').value;
session.call("com.example.keyvalue.get", [key]).then(
function (value) {
console.log(value);
document.getElementById('form_value1').value = value.value1;
document.getElementById('form_value2').value = value.value2;
document.getElementById('form_value3').checked = value.value3;
}
);
}
function deleteKey() {
var key = document.getElementById('form_key').value;
session.call("com.example.keyvalue.set", [key]).then(
function () {
log("Key-Value deleted!");
}
);
}
function getAll() {
session.call("com.example.keyvalue.get").then(
function (res) {
console.log(res);
}
);
}
function deleteAll() {
session.call("com.example.keyvalue.set").then(
function () {
log("All Key-Values deleted!");
}
);
}
</script>
</body>
</html>
|