Struct holochain_websocket::WebsocketSender
source · pub struct WebsocketSender { /* private fields */ }
Expand description
The sender half of an active connection.
Example
use holochain_serialized_bytes::prelude::*;
use holochain_websocket::*;
use std::time::Duration;
use url2::url2;
#[derive(serde::Serialize, serde::Deserialize, SerializedBytes, Debug)]
struct TestMessage(pub String);
#[tokio::main]
async fn main() {
let (mut send, _) = connect(
url2!("ws://127.0.0.1:12345"),
std::sync::Arc::new(WebsocketConfig::default()),
)
.await
.unwrap();
send.signal(TestMessage("Hey".to_string())).await.unwrap();
let resp: TestMessage = send.request(TestMessage("Hey".to_string())).await.unwrap();
println!("Got {:?}", resp);
match send
.request_timeout(TestMessage("Hey".to_string()), Duration::from_secs(1))
.await
{
Ok(r) => {
let resp: TestMessage = r;
println!("Got {:?}", resp);
}
Err(WebsocketError::RespTimeout) => eprintln!("Failed to get a response in 1 second"),
Err(e) => eprintln!("Got an error sending a request {:?}", e),
}
}
Implementations§
source§impl WebsocketSender
impl WebsocketSender
sourcepub async fn request_timeout<I, O>(
&mut self,
msg: I,
timeout: Duration
) -> WebsocketResult<O>where
I: Debug + Serialize,
O: Debug + DeserializeOwned,
WebsocketError: From<SerializedBytesError>,
pub async fn request_timeout<I, O>( &mut self, msg: I, timeout: Duration ) -> WebsocketResult<O>where I: Debug + Serialize, O: Debug + DeserializeOwned, WebsocketError: From<SerializedBytesError>,
Make a request to for the other side to respond to.
sourcepub async fn request<I, O>(&mut self, msg: I) -> WebsocketResult<O>where
I: Debug + Serialize,
O: Debug + DeserializeOwned,
WebsocketError: From<SerializedBytesError>,
pub async fn request<I, O>(&mut self, msg: I) -> WebsocketResult<O>where I: Debug + Serialize, O: Debug + DeserializeOwned, WebsocketError: From<SerializedBytesError>,
Make a request to for the other side to respond to.
Note:
There is no timeouts in this code. You either need to wrap
this future in a timeout or use WebsocketSender::request_timeout
.
sourcepub async fn signal<I, E>(&mut self, msg: I) -> WebsocketResult<()>where
I: Debug,
WebsocketError: From<E>,
SerializedBytes: TryFrom<I, Error = E>,
pub async fn signal<I, E>(&mut self, msg: I) -> WebsocketResult<()>where I: Debug, WebsocketError: From<E>, SerializedBytes: TryFrom<I, Error = E>,
Send a message to the other side that doesn’t require a response.
There is no guarantee this message will arrive. If you need confirmation
of receipt use WebsocketSender::request
.
Trait Implementations§
source§impl Clone for WebsocketSender
impl Clone for WebsocketSender
source§fn clone(&self) -> WebsocketSender
fn clone(&self) -> WebsocketSender
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for WebsocketSender
impl Send for WebsocketSender
impl Sync for WebsocketSender
impl Unpin for WebsocketSender
impl !UnwindSafe for WebsocketSender
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more