Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions examples/experimental/light-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ <h2>Remote Peers</h2>
</button>
<br />
<div id="messages"></div>
<br />
<button disabled id="periodicSendButton" type="button">
Send message using Light Push every 5 seconds
</button>

<div>
<h2>Store</h2>
Expand Down Expand Up @@ -95,6 +99,7 @@ <h2>Store</h2>
const messagesDiv = document.getElementById("messages");
const textInput = document.getElementById("textInput");
const sendButton = document.getElementById("sendButton");
const periodicSendButton = document.getElementById("periodicSendButton");
const getPeersButton = document.getElementById("getPeersButton");
const peersSelector = document.getElementById("peer-select");

Expand Down Expand Up @@ -166,6 +171,7 @@ <h2>Store</h2>
// Enable send and subscribe inputs as we are now connected to a peer
textInput.disabled = false;
sendButton.disabled = false;
periodicSendButton.disabled = false;
subscribeButton.disabled = false;
queryStoreButton.disabled = false;
});
Expand Down Expand Up @@ -232,13 +238,36 @@ <h2>Store</h2>
sendButton.onclick = async () => {
const text = textInput.value;

await node.lightPush.send(encoder, {
const result = await node.lightPush.send(encoder, {
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;
};

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 () => {
await getPeers(statusDiv, remoteMultiAddrDiv);
};
Expand Down
Loading