working example for mycelium
Some checks failed
Rhai Tests / Run Rhai Tests (pull_request) Has been cancelled

This commit is contained in:
Maxime Van Hees
2025-05-13 11:52:31 +02:00
parent 3225b3f029
commit 3e64a53a83
3 changed files with 106 additions and 82 deletions

View File

@@ -22,9 +22,9 @@ try {
print("No peers connected.");
} else {
for peer in peers {
print(`Peer ID: ${peer.id}`);
print(` Address: ${peer.address}`);
print(` Connected: ${peer.connected}`);
print(`Peer Endpoint: ${peer.endpoint.proto}://${peer.endpoint.socketAddr}`);
print(` Type: ${peer.type}`);
print(` Connection State: ${peer.connectionState}`);
print(` Bytes sent: ${peer.txBytes}`);
print(` Bytes received: ${peer.rxBytes}`);
}
@@ -35,7 +35,7 @@ try {
// Add a new peer
print("\nAdding a new peer:");
let new_peer_address = "tcp://185.69.166.8:9651";
let new_peer_address = "tcp://65.21.231.58:9651";
try {
let result = mycelium_add_peer(api_url, new_peer_address);
print(`Peer added: ${result.success}`);
@@ -80,47 +80,51 @@ try {
}
// Send a message
print("\nSending a message:");
let destination = "400:1234:5678:9abc:def0:1234:5678:9abc";
let topic = "test";
let message = "Hello from Rhai!";
let deadline_secs = 60;
// TO SEND A MESSAGE FILL IN THE DESTINATION IP ADDRESS
// -----------------------------------------------------//
// print("\nSending a message:");
// let destination = < FILL IN CORRECT DEST IP >
// let topic = "test";
// let message = "Hello from Rhai!";
// let deadline_secs = 60;
try {
let result = mycelium_send_message(api_url, destination, topic, message, deadline_secs);
print(`Message sent: ${result.success}`);
if result.id {
print(`Message ID: ${result.id}`);
}
} catch(err) {
print(`Error sending message: ${err}`);
}
// try {
// let result = mycelium_send_message(api_url, destination, topic, message, deadline_secs);
// print(`Message sent: ${result.success}`);
// if result.id {
// print(`Message ID: ${result.id}`);
// }
// } catch(err) {
// print(`Error sending message: ${err}`);
// }
// Receive messages
print("\nReceiving messages:");
let receive_topic = "test";
let count = 5;
// RECEIVING MESSAGES SHOULD BE DONE ON THE DESTINATION NODE FROM THE CALL ABOVE
// -----------------------------------------------------------------------------//
// print("\nReceiving messages:");
// let receive_topic = "test";
// let count = 5;
try {
let messages = mycelium_receive_messages(api_url, receive_topic, count);
// try {
// let messages = mycelium_receive_messages(api_url, receive_topic, count);
if messages.is_empty() {
print("No messages received.");
} else {
for msg in messages {
print(`Message from: ${msg.source}`);
print(` Topic: ${msg.topic}`);
print(` Content: ${msg.content}`);
print(` Timestamp: ${msg.timestamp}`);
}
}
} catch(err) {
print(`Error receiving messages: ${err}`);
}
// if messages.is_empty() {
// print("No messages received.");
// } else {
// for msg in messages {
// print(`Message from: ${msg.source}`);
// print(` Topic: ${msg.topic}`);
// print(` Content: ${msg.content}`);
// print(` Timestamp: ${msg.timestamp}`);
// }
// }
// } catch(err) {
// print(`Error receiving messages: ${err}`);
// }
// Remove a peer
print("\nRemoving a peer:");
let peer_id = "some-peer-id"; // Replace with an actual peer ID
let peer_id = "tcp://65.21.231.58:9651"; // This is the peer we added earlier
try {
let result = mycelium_remove_peer(api_url, peer_id);
print(`Peer removed: ${result.success}`);