nym-client: adding js example so I can see if sending is working at all.

This commit is contained in:
Dave Hrycyszyn
2020-04-14 15:49:21 +01:00
parent 3c8aad49fc
commit 25cfa26c56
2 changed files with 76 additions and 0 deletions
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<script src="socket.js" async></script>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nym Client Demo</title>
</head>
<body>
<p>
<div id="output">Loading JavaScript...</div>
</p>
<p>
Fetched messages: <span id="fetch">none<div>
</p>
</body>
</html>
@@ -0,0 +1,56 @@
display("Initialising...");
var port = '9001' // client websocket listens on 9001 by default, change if yours is different
var connection = new WebSocket('ws://127.0.0.1:' + port);
// When the connection is open, send some data to the server
connection.onopen = function () {
display('Opened connection to Nym client websocket.');
};
// Log errors
connection.onerror = function (error) {
display('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e) {
let response = JSON.parse(e.data);
if (response.type == "error") {
display('Server responded with error: ' + response.message);
} else if (response.type == "fetch") {
displayFetch(response.messages);
}
};
function display(message) {
document.getElementById("output").innerHTML = message;
}
function displayFetch(message) {
document.getElementById("fetch").innerHTML = message;
}
display("Nym client demo started.");
var sequenceNum = 0;
// window.setInterval(function () {
// var message = {
// type: "send",
// message: "FOOMP " + sequenceNum,
// recipient_address: "B5D5wfetpXZ6qSQ7y8Cyi6mHttS3doiwDdvuaxin2NWC"
// }
// connection.send(JSON.stringify(message));
// display("Sent message: " + message.message + " to " + message.recipient_address);
// sequenceNum += 1;
// }, 500);
window.setInterval(function () {
var message = {
type: "fetch",
}
displayFetch("fetching...");
connection.send(JSON.stringify(message));
sequenceNum += 1;
}, 1000);