feat: add button to periodically push message in light-js

This commit is contained in:
Arseniy Klempner 2024-05-07 21:03:15 -07:00
parent d62e3579fd
commit 903d5d7a85
No known key found for this signature in database
GPG Key ID: 51653F18863BD24B
1 changed files with 31 additions and 2 deletions

View File

@ -59,6 +59,10 @@
</button> </button>
<br /> <br />
<div id="messages"></div> <div id="messages"></div>
<br />
<button disabled id="periodicSendButton" type="button">
Send message using Light Push every 5 seconds
</button>
<div> <div>
<h2>Store</h2> <h2>Store</h2>
@ -95,6 +99,7 @@
const messagesDiv = document.getElementById("messages"); const messagesDiv = document.getElementById("messages");
const textInput = document.getElementById("textInput"); const textInput = document.getElementById("textInput");
const sendButton = document.getElementById("sendButton"); const sendButton = document.getElementById("sendButton");
const periodicSendButton = document.getElementById("periodicSendButton");
const getPeersButton = document.getElementById("getPeersButton"); const getPeersButton = document.getElementById("getPeersButton");
const peersSelector = document.getElementById("peer-select"); const peersSelector = document.getElementById("peer-select");
@ -166,6 +171,7 @@
// Enable send and subscribe inputs as we are now connected to a peer // Enable send and subscribe inputs as we are now connected to a peer
textInput.disabled = false; textInput.disabled = false;
sendButton.disabled = false; sendButton.disabled = false;
periodicSendButton.disabled = false;
subscribeButton.disabled = false; subscribeButton.disabled = false;
queryStoreButton.disabled = false; queryStoreButton.disabled = false;
}); });
@ -232,13 +238,36 @@
sendButton.onclick = async () => { sendButton.onclick = async () => {
const text = textInput.value; const text = textInput.value;
await node.lightPush.send(encoder, { const result = await node.lightPush.send(encoder, {
payload: utf8ToBytes(text), payload: utf8ToBytes(text),
}); });
console.log("Message sent!"); if(result.failures.length > 0 || result.successes.length === 0) {
console.log("Message failed to send", result.failures);
} else {
console.log("Message sent to the following peers:");
console.log(result.successes.map(s => s.toString()));
}
textInput.value = null; textInput.value = null;
}; };
periodicSendButton.onclick = async () => {
let i = 0;
while(true) {
const result = await node.lightPush.send(encoder, {
payload: utf8ToBytes(`message ${i}`)
});
if(result.failures.length > 0 || result.successes.length === 0) {
console.log(`Message ${i} failed to send`, result.failures);
} else {
console.log(`Message ${i} sent to the following peers:`);
console.log(result.successes.map(s => s.toString()));
}
i++;
await new Promise(resolve => setTimeout(resolve, 5000));
}
};
getPeersButton.onclick = async () => { getPeersButton.onclick = async () => {
await getPeers(statusDiv, remoteMultiAddrDiv); await getPeers(statusDiv, remoteMultiAddrDiv);
}; };