57c43a1f6b
* Changed default listening port to something slightly more meaningful * Removed TCP socket and made websocket the default option (as opposed to 'None') * Updated template * Updated ReceivedBuffer to allow direct message forwarding * ignoring vscode directory * Push messages mechanism for websocket client-clients * Removed flawed chunking example * ... but added bunch of websocket examples in return! * Moves js example directory * Cargo fmt * Removed old listener code
63 lines
2.5 KiB
Rust
63 lines
2.5 KiB
Rust
use bs58;
|
|
use futures::{SinkExt, StreamExt};
|
|
use nym_client::websocket::{BinaryClientRequest, ClientRequest, ServerResponse};
|
|
use nymsphinx::DestinationAddressBytes;
|
|
use std::convert::TryFrom;
|
|
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let uri = "ws://localhost:1977";
|
|
let (mut ws_stream, _) = connect_async(uri).await.unwrap();
|
|
|
|
let self_address_request = ClientRequest::SelfAddress;
|
|
ws_stream.send(self_address_request.into()).await.unwrap();
|
|
|
|
let raw_response = ws_stream.next().await.unwrap().unwrap();
|
|
// what we received now is just a json, but we know it's exact format
|
|
// so might as well use that
|
|
let response = match raw_response {
|
|
Message::Text(txt_msg) => ServerResponse::try_from(txt_msg).unwrap(),
|
|
_ => panic!("received an unexpected response type!"),
|
|
};
|
|
|
|
let self_address = match response {
|
|
ServerResponse::SelfAddress { address } => address,
|
|
_ => panic!("received an unexpected response type!"),
|
|
};
|
|
println!("our address is: {}", self_address.clone());
|
|
|
|
let mut address_bytes = [0; 32];
|
|
bs58::decode(self_address).into(&mut address_bytes).unwrap();
|
|
|
|
// this is equivalent to just sending bs58 decoding of the address, which is always 32 bytes
|
|
let decoded_address = DestinationAddressBytes::from_bytes(address_bytes);
|
|
let read_data = std::fs::read("examples/dummy_file").unwrap();
|
|
|
|
let send_request = BinaryClientRequest::Send {
|
|
recipient_address: decoded_address,
|
|
data: read_data,
|
|
};
|
|
|
|
println!("sending content of 'dummy_file' over the mix network...");
|
|
ws_stream.send(send_request.into()).await.unwrap();
|
|
|
|
let raw_send_confirmation = ws_stream.next().await.unwrap().unwrap();
|
|
let _send_confirmation = match raw_send_confirmation {
|
|
Message::Text(txt_msg) => match ServerResponse::try_from(txt_msg).unwrap() {
|
|
ServerResponse::Send => (),
|
|
_ => panic!("received an unexpected response type!"),
|
|
},
|
|
_ => panic!("received an unexpected response type!"),
|
|
};
|
|
|
|
println!("waiting to receive the 'dummy_file' from the mix network...");
|
|
let raw_message = ws_stream.next().await.unwrap().unwrap();
|
|
let message = match raw_message {
|
|
Message::Binary(bin_payload) => bin_payload,
|
|
_ => panic!("received an unexpected response type!"),
|
|
};
|
|
|
|
println!("writing the file back to the disk!");
|
|
std::fs::write("examples/received_file", message).unwrap();
|
|
}
|