Struct holochain_websocket::WebsocketSender[][src]

pub struct WebsocketSender { /* fields omitted */ }
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

Make a request to for the other side to respond to.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more