Struct Client

Source
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

Source

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.

Source

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.

Source

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.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V