Skip to main content

gproxy_protocol/connection/
websocket.rs

1use std::{fmt, pin::Pin};
2
3use futures_core::Stream;
4use futures_sink::Sink;
5
6use super::{Bytes, TransportError};
7
8/// A complete WebSocket data message or control frame. The transport adapter
9/// handles wire fragmentation, masking, and protocol validation.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum WsFrame {
12    Text(String),
13    Binary(Bytes),
14    Ping(Bytes),
15    Pong(Bytes),
16    Close(Option<WsClose>),
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct WsClose {
21    pub code: u16,
22    pub reason: String,
23}
24
25#[cfg(not(target_arch = "wasm32"))]
26pub type WsReceiver = Pin<Box<dyn Stream<Item = Result<WsFrame, TransportError>> + Send + 'static>>;
27
28#[cfg(target_arch = "wasm32")]
29pub type WsReceiver = Pin<Box<dyn Stream<Item = Result<WsFrame, TransportError>> + 'static>>;
30
31#[cfg(not(target_arch = "wasm32"))]
32pub type WsSender = Pin<Box<dyn Sink<WsFrame, Error = TransportError> + Send + 'static>>;
33
34#[cfg(target_arch = "wasm32")]
35pub type WsSender = Pin<Box<dyn Sink<WsFrame, Error = TransportError> + 'static>>;
36
37/// An established connection. Sending and receiving are independent; there is
38/// no request/response pairing. The HTTP handshake is represented separately
39/// by [`super::WireRequest`] and [`super::WireResponse`].
40pub struct WebSocket {
41    pub incoming: WsReceiver,
42    pub outgoing: WsSender,
43}
44
45impl fmt::Debug for WebSocket {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.write_str("WebSocket { incoming: .., outgoing: .. }")
48    }
49}