Skip to main content

websocket_transport

Function websocket_transport 

Source
pub async fn websocket_transport(
    address: &str,
    version: OcppVersion,
    options: Option<ConnectOptions<'_>>,
) -> Result<(Box<dyn TransportSink>, Box<dyn TransportStream>), TransportError>
Expand description

Opens one WebSocket connection to address speaking version, and hands back the transport halves a Reconnector is required to return - so a custom reconnector can redial without reimplementing this crate’s WebSocket plumbing.

options covers credentials and TLS for this dial; its reconnect/reconnector fields are ignored, since this opens a bare transport rather than a Client. version must be the one already negotiated on the connection being replaced - the client’s handlers are bound to that version, so redialling a different one would leave it speaking the wrong protocol.

/// Redials whatever address it is currently pointed at.
struct SwitchableReconnector(Arc<Mutex<String>>);

impl Reconnector for SwitchableReconnector {
    fn connect<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<(Box<dyn TransportSink>, Box<dyn TransportStream>), TransportError>> + Send + 'a>> {
        Box::pin(async move {
            let address = self.0.lock().unwrap().clone();
            websocket_transport(&address, OcppVersion::V1_6, None).await
        })
    }
}