viewpoint_core/network/websocket/
frame.rs

1//! WebSocket frame types.
2
3use viewpoint_cdp::protocol::WebSocketFrame as CdpWebSocketFrame;
4
5/// A WebSocket message frame.
6#[derive(Debug, Clone)]
7pub struct WebSocketFrame {
8    /// The frame opcode (1 for text, 2 for binary).
9    opcode: u8,
10    /// The frame payload data.
11    payload_data: String,
12}
13
14impl WebSocketFrame {
15    /// Create a new WebSocket frame.
16    pub(crate) fn new(opcode: u8, payload_data: String) -> Self {
17        Self {
18            opcode,
19            payload_data,
20        }
21    }
22
23    /// Create a WebSocket frame from CDP frame data.
24    pub(crate) fn from_cdp(cdp_frame: &CdpWebSocketFrame) -> Self {
25        Self {
26            opcode: cdp_frame.opcode as u8,
27            payload_data: cdp_frame.payload_data.clone(),
28        }
29    }
30
31    /// Get the frame opcode.
32    ///
33    /// Common opcodes:
34    /// - 1: Text frame
35    /// - 2: Binary frame
36    /// - 8: Close frame
37    /// - 9: Ping frame
38    /// - 10: Pong frame
39    pub fn opcode(&self) -> u8 {
40        self.opcode
41    }
42
43    /// Get the frame payload data.
44    pub fn payload(&self) -> &str {
45        &self.payload_data
46    }
47
48    /// Check if this is a text frame.
49    pub fn is_text(&self) -> bool {
50        self.opcode == 1
51    }
52
53    /// Check if this is a binary frame.
54    pub fn is_binary(&self) -> bool {
55        self.opcode == 2
56    }
57}