WebSocket into Emit/Recieve (How to publish a value into Recieve and how to recieve it?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// trying to create the websocket sync connection and keep it open | |
let mut client = ClientBuilder::new("ws://localhost:3401") | |
.unwrap() | |
.connect_insecure() | |
.unwrap(); | |
// this doesn't compile due to the function not accepting the dynamic 'client' but is this how the send function should look? | |
fn set_to_dht(_signal: String) { | |
let json = serde_json::json!( | |
{"id": "signal", | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": {"instance_id": "test-instance", | |
"zome": "signal_node", | |
"function": "send", | |
"args": {"_address": "HcSCIjn77EYm9C6gqsxuisP95WBnw5jf5QCAVAZ5dizvamsfDZSAXi35BVj5bsz", | |
"_message": _signal, | |
} | |
}}); | |
let message = Message::text(json.to_string()); | |
&client.send_message(&message).unwrap(); // Send message | |
} | |
// this doesn't compile as I need to sort out the issue with calling the 'client' socket but is this how my receive function should look? | |
fn rec_from_dht() { | |
let json = serde_json::json!( | |
{"id": "signal", | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": {"instance_id": "test-instance", | |
"zome": "signal_node", | |
"function": "receive", | |
"args": {} | |
}}); | |
let message = Message::text(json.to_string()); | |
&client.send_message(&message).unwrap(); | |
} | |
// set a value then recieve it | |
set_to_dht("Hello World!".to_string()); | |
// for the receive function in the zome is this right that I should open a thread, keep it open and loop running the receive call similar to this? | |
let (tx, rx) = channel(); | |
thread::spawn(move || { | |
loop { | |
let listen = rec_from_dht(); | |
tx.send(listen).unwrap(); | |
thread::sleep(time::Duration::from_millis(18000)); | |
} | |
}); | |
let mut _current_value = rx.recv().unwrap(); //when the send function fires this thread and value is updated? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment