Files
hpcore/examples/js_client/browser-example.html
Ravin Perera 0de7983504 User outputs round limit. (#241)
Implemented user outputs round limit and upgraded the contract libraries to support the configs.
2021-02-15 14:23:47 +05:30

99 lines
3.9 KiB
HTML

<html>
<head>
<title>HotPocket browser example</title>
<script src="hp-client-lib.js"></script>
<!--Hot Pocket client library requires libsodium js for cryptographic operations.-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/libsodium-wrappers/0.5.4/sodium.min.js"
integrity="sha512-oRfU7aik4u7f0dPAKgOyA4+bb/YRGfAaD5RA4Z3Mb2ycPcGDs+k8qAnDNd7ouruoqlIHSuGVaTTlEs91Gvd37A=="
crossorigin="anonymous"></script>
<!--Hot Pocket client library requires blake3 for hashing operations.-->
<script type="module">
import init from 'https://cdn.jsdelivr.net/npm/blake3@2.1.4/browser-async.js';
init().then(blake3 => HotPocket.setBlake3(blake3)).catch(console.error);
</script>
<script>
async function testFunc() {
const keys = await HotPocket.generateKeys(); // Can provide existing hex private key as parameter as well.
// Simple connection to single server without any validations.
const hpc = await HotPocket.createClient(["wss://localhost:8081"], keys);
// Maintain multiple connections with contract id/version and trusted server key validation.
// const hpc = await HotPocket.createClient(
// [
// "wss://localhost:8081",
// "wss://localhost:8082",
// "wss://localhost:8083"
// ],
// keys,
// {
// contractId: "3c349abe-4d70-4f50-9fa6-018f1f2530ab",
// contractVersion: "1.0",
// trustedServerKeys: [
// "ed5597c207bbd251997b7133d5d83a2c6ab9600810edf0bdb43f4004852b8c9e17",
// "ed0b2ffd75b67c3979d3c362d8350ec190f053fa27d3dfcb2eced426efd1d3affc",
// "edd2e1a817387d68adf8adb1d0b339e3f04868c3c81bf6a7472647f10657e31aa1"
// ],
// protocol: HotPocket.protocols.json,
// requiredConnectionCount: 2,
// connectionTimeoutMs: 5000
// });
// We'll register for HotPocket events before connecting.
// This will get fired if HP server disconnects unexpectedly.
hpc.on(HotPocket.events.disconnect, () => {
console.log('Disconnected');
})
// This will get fired as servers connects/disconnects.
hpc.on(HotPocket.events.connectionChange, (server, action) => {
console.log(server + " " + action);
})
// This will get when the unl public key list changes.
hpc.on(HotPocket.events.unlChange, (unl) => {
console.log("New unl received: " + JSON.stringify(unl)); // unl is an array of hex public keys.
})
// This will get fired when contract sends an output.
hpc.on(HotPocket.events.contractOutput, (output) => {
console.log("Contract output>> " + output);
})
// This will get fired when contract sends a read response.
hpc.on(HotPocket.events.contractReadResponse, (response) => {
console.log("Contract read response>> " + response);
})
// Establish HotPocket connection.
if (!await hpc.connect()) {
console.log('Connection failed.');
return;
}
console.log('HotPocket Connected.');
hpc.sendContractReadRequest("Hello");
hpc.sendContractInput("World!").then(status => {
if (status != "ok")
console.log(status);
});
// When we need to close HP connection:
// await hpc.close();
}
testFunc();
</script>
</head>
<body>
HotPocket browser client example page. Use developer tools to see activity.
</body>
</html>