pub struct Client { /* private fields */ }
Expand description
§Tokio-based for the Zeek WebSocket API
Client
implements an async client for the Zeek WebSocket API. It is intended to be run
inside a [tokio
] runtime. The general workflow is to build a client with the ClientConfig
builder interface, and then either publish or receive events.
§Example
use anyhow::Result;
use zeek_websocket::client::ClientConfig;
pub use zeek_websocket::Event;
#[tokio::main]
async fn main() -> Result<()> {
let mut client = ClientConfig::builder()
.app_name("my_client_application")
.subscribe("/info")
.endpoint("ws://127.0.0.1:8080/v1/messages/json".try_into()?)
.build()?;
client
.publish_event("/ping", Event::new("ping", vec!["abc"]))
.await;
loop {
// Client automatically receives events on topics it sent to.
if let Some((_topic, event)) = client.receive_event().await? {
eprintln!("{event:?}");
break;
}
}
Ok(())
}
Implementations§
Source§impl Client
impl Client
Sourcepub async fn publish_event<S: Into<String>>(&mut self, topic: S, event: Event)
pub async fn publish_event<S: Into<String>>(&mut self, topic: S, event: Event)
Publish an Event
to topic
. The client will be automatically subscribed to the topic
if is not already.
Sourcepub async fn receive_event(&mut self) -> Result<Option<(String, Event)>, Error>
pub async fn receive_event(&mut self) -> Result<Option<(String, Event)>, Error>
Receive the next Event
or Error
.
If an event was received it will be returned as Ok(Some((topic, event)))
.
§Errors
Might return a ProtocolError
from the underlying binding, e.g., Zeek, or an
transport-related error.
Sourcepub async fn subscribe<S: Into<String>>(&mut self, topic: S)
pub async fn subscribe<S: Into<String>>(&mut self, topic: S)
Subscribe to a topic.
This is a noop if the client is already subscribed.
Sourcepub async fn unsubscribe(&mut self, topic: &str) -> bool
pub async fn unsubscribe(&mut self, topic: &str) -> bool
Unsubscribe from a topic.
This is a noop if the client was not subscribed.
Auto Trait Implementations§
impl !Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
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