Skip to content

Instantly share code, notes, and snippets.

@simwilso
Last active December 11, 2019 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simwilso/b0f7363d1fa06863f3384a401d38a508 to your computer and use it in GitHub Desktop.
Save simwilso/b0f7363d1fa06863f3384a401d38a508 to your computer and use it in GitHub Desktop.
WebSocket into Emit/Recieve (How to publish a value into Recieve and how to recieve it?)
// 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