Files
hpcore/examples/js_client/browser-test.html
Ravin Perera f2ed9040c0 User protocol upgrade and js client lib. (#191)
* Unified js client lib for browser and nodejs.
* Client lib multiple connections support.
* Implemented server challenge response.
* Contract guid and version validation.
* Server key validation.
* User json message encoding improvements.
2020-12-11 11:02:58 +05:30

79 lines
3.1 KiB
HTML

<html>
<head>
<title>HotPocket test page</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>
<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 server key validation.
// const options = {
// contractId: "3c349abe-4d70-4f50-9fa6-018f1f2530ab",
// contractVersion: "1.0",
// validServerKeys: [
// "ed5597c207bbd251997b7133d5d83a2c6ab9600810edf0bdb43f4004852b8c9e17",
// "ed0b2ffd75b67c3979d3c362d8350ec190f053fa27d3dfcb2eced426efd1d3affc",
// "edd2e1a817387d68adf8adb1d0b339e3f04868c3c81bf6a7472647f10657e31aa1"
// ],
// protocol: HotPocket.protocols.json,
// requiredConnectionCount: 2,
// connectionTimeoutMs: 5000
// };
// const hpc = await HotPocket.createClient(
// [
// "wss://localhost:8081",
// "wss://localhost:8082",
// "wss://localhost:8083"
// ], keys, options);
if (!await hpc.connect()) {
console.log('Connection failed.');
return;
}
console.log('HotPocket Connected.');
// 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 after the initial connection establishment.
hpc.on(HotPocket.events.connectionChange, (server, action) => {
console.log(server + " " + action);
})
// 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);
})
hpc.sendContractReadRequest("Hello");
hpc.sendContractInput("World!")
// When we need to close HP connection:
// await hpc.close();
}
testFunc();
</script>
</head>
<body>
HotPocket browser client test page.
</body>
</html>