Skip to main content

Crate pdk_websockets_lib

Crate pdk_websockets_lib 

Source
Expand description

PDK WebSockets Library

Library for decoding and encoding WebSocket frames in Flex Gateway custom policies. It wraps [websocket-sans-io] to provide ergonomic frame-level access for policies that operate on WebSocket upgrade connections.

§Primary types

  • Frame: a single WebSocket frame with its payload and metadata
  • FrameType: the kind of frame (Text, Binary, Ping, Pong, etc.)
  • Decoder: incrementally decodes raw bytes into Frames
  • Encoder: re-encodes a collection of Frames into bytes
  • SinkResult: outcome of feeding bytes to the Decoder

§Example

use pdk_websockets::{Decoder, Encoder, Frame, FrameType, SinkResult};

let mut decoder = Decoder::default();
match decoder.sink(raw_bytes) {
    SinkResult::MidFrame => { /* pause and wait for more bytes */ }
    SinkResult::Complete(mut frames) => {
        // inspect frames
        if let Some(frame) = frames.first() {
            if let FrameType::Text = frame.frame_type() {
                let text = String::from_utf8_lossy(frame.data());
            }
        }
        // modify frames, then re-encode
        frames.push(Frame::ping());
        let bytes = Encoder::default().encode(frames);
    }
}

Structs§

Decoder
Incrementally decodes raw bytes into Frames.
Encoder
Encodes a collection of Frames back into raw bytes suitable for writing to a WebSocket connection.
Frame
A single WebSocket frame.
UpgradeTracker

Enums§

FrameType
The kind of a WebSocket frame, derived from its opcode.
SinkResult
The result of feeding bytes to Decoder::sink.