pub struct CdpClient { /* private fields */ }Expand description
Connected CDP client. Cheap to share via the trait object on a
backend — internal state is Arc-backed.
Dropping the client aborts its read task; pending in-flight calls
surface CdpError::Closed.
Implementations§
Source§impl CdpClient
impl CdpClient
Sourcepub async fn connect(url: &str) -> Result<Self, CdpError>
pub async fn connect(url: &str) -> Result<Self, CdpError>
Open a WebSocket to url (ws:// or wss://) and start the read
loop. Returns once the handshake completes.
§Errors
CdpError::WebSocket on handshake, DNS, or TLS failure.
Sourcepub async fn execute<P, R>(
&self,
method: &'static str,
params: P,
session_id: Option<&str>,
timeout: Duration,
) -> Result<R, CdpError>where
P: Serialize,
R: DeserializeOwned,
pub async fn execute<P, R>(
&self,
method: &'static str,
params: P,
session_id: Option<&str>,
timeout: Duration,
) -> Result<R, CdpError>where
P: Serialize,
R: DeserializeOwned,
Send Domain.cmd with params, await the matching response, and
decode the result field as R.
session_id scopes the call to a flat-attached target (see
Target.attachToTarget with flatten: true); pass None for
browser-wide commands.
§Errors
CdpError::Closedif the client has been closed.CdpError::WebSocketif the send fails on the wire.CdpError::Timeoutif no response arrives withintimeout.CdpError::Remoteif CDP replied with an error object.CdpError::Decodeif the result didn’t deserialise asR.
Sourcepub fn subscribe_events(&self) -> Receiver<CdpEvent>
pub fn subscribe_events(&self) -> Receiver<CdpEvent>
Subscribe to every event the read loop dispatches.
Slow subscribers may lag — broadcast::Receiver::recv returns
broadcast::error::RecvError::Lagged in that case. Filter the
stream on .method and .session_id to scope to the events you
care about.
Sourcepub async fn wait_for_event<F>(
&self,
predicate: F,
timeout: Duration,
what: &'static str,
) -> Result<CdpEvent, CdpError>
pub async fn wait_for_event<F>( &self, predicate: F, timeout: Duration, what: &'static str, ) -> Result<CdpEvent, CdpError>
Convenience: open a fresh subscription and drive it until
predicate returns true, or timeout elapses.
Has a built-in race: if the event you’re waiting for fires
between the action that triggers it and this call, it’s missed
(the broadcast channel doesn’t replay history). For event waits
that follow a triggering command, prefer
wait_for_event_on with a
subscription opened before the trigger.
§Errors
CdpError::Timeout if timeout elapses; CdpError::Closed
if the underlying stream ends first.
Sourcepub async fn wait_for_event_on<F>(
rx: &mut Receiver<CdpEvent>,
predicate: F,
timeout: Duration,
what: &'static str,
) -> Result<CdpEvent, CdpError>
pub async fn wait_for_event_on<F>( rx: &mut Receiver<CdpEvent>, predicate: F, timeout: Duration, what: &'static str, ) -> Result<CdpEvent, CdpError>
Drive an already-opened subscription until predicate returns
true. Use this when you need to subscribe before sending the
command that triggers the event — otherwise the event can fire
before your subscription exists and you’ll deadlock.
§Errors
Same as wait_for_event.
Sourcepub async fn close(self)
pub async fn close(self)
Best-effort: close the WebSocket politely. Pending calls
surface CdpError::Closed. Always safe to call; subsequent
calls are no-ops.