alloy_transport_ws/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#![doc = include_str!("../README.md")]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
    html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

#[macro_use]
extern crate tracing;

use alloy_pubsub::ConnectionInterface;

#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(not(target_arch = "wasm32"))]
pub use native::{WebSocketConfig, WsConnect};

#[cfg(not(target_arch = "wasm32"))]
use rustls as _;

#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
pub use wasm::WsConnect;

/// An ongoing connection to a backend.
///
/// Users should NEVER instantiate a backend directly. Instead, they should use
/// [`PubSubConnect`] to get a running service with a running backend.
///
/// [`PubSubConnect`]: alloy_pubsub::PubSubConnect
#[derive(Debug)]
pub struct WsBackend<T> {
    /// The websocket connection.
    pub(crate) socket: T,

    /// The interface to the connection.
    pub(crate) interface: ConnectionInterface,
}

impl<T> WsBackend<T> {
    /// Handle inbound text from the websocket.
    #[allow(clippy::result_unit_err)]
    pub fn handle_text(&mut self, text: &str) -> Result<(), ()> {
        trace!(%text, "received message from websocket");

        match serde_json::from_str(text) {
            Ok(item) => {
                trace!(?item, "deserialized message");
                if let Err(err) = self.interface.send_to_frontend(item) {
                    error!(item=?err.0, "failed to send deserialized item to handler");
                    return Err(());
                }
            }
            Err(err) => {
                error!(%err, "failed to deserialize message");
                return Err(());
            }
        }
        Ok(())
    }
}