Module protocol

Source
Expand description

§Sans I/O-style protocol wrapper for the Zeek API

Instead of providing a full-fledged client Binding encapsulates the Zeek WebSocket protocol sans I/O style. It provides the following methods:

A full client implementation will typically implement some form of event loop.

§Example

use zeek_websocket::*;

// Open an underlying WebSocket connection to a Zeek endpoint.
let (mut socket, _) = tungstenite::connect("ws://127.0.0.1:8080/v1/messages/json")?;

// Create a connection.
let mut conn = Binding::new(&["/ping"]);

// The event loop.
loop {
    // If we have any outgoing messages send at least one.
    if let Some(data) = conn.outgoing() {
        socket.send(tungstenite::Message::binary(data))?;
    }

    // Receive the next message and handle it.
    if let Ok(msg) = socket.read()?.try_into() {
        conn.handle_incoming(msg);
    }

    // If we received a `ping` event, respond with a `pong`.
    if let Some((topic, event)) = conn.receive_event()? {
        if event.name == "ping" {
            conn.publish_event(topic, Event::new("pong", event.args));
        }
    }
}

Structs§

Binding
Protocol wrapper for a Zeek WebSocket connection.
Inbox
Receiving side of a Binding.
Outbox
Sending side of Binding.

Enums§

ProtocolError
Error enum for protocol-related errors.