1use bytes::Bytes;
2use std::borrow::Cow;
3
4pub const PROTOCOL_WEBSOCKET: &str = "websocket";
5pub const EVENT_WEBSOCKET: &str = "websocket";
6
7#[non_exhaustive]
8#[derive(Default, Clone, Debug)]
9pub struct WebSocketScope {
10 pub subprotocols: Vec<Cow<'static, str>>,
11}
12
13#[non_exhaustive]
14#[derive(Clone, Debug)]
15pub enum WebSocketEvent {
16 Connect(Connect),
18 Accept(Accept),
20 TextFrame(TextFrame),
22 BinaryFrame(BinaryFrame),
24 Disconnect(Disconnect),
26 Close(Close),
28}
29
30#[non_exhaustive]
31#[derive(Default, Clone, Debug)]
32pub struct Connect {}
33
34#[non_exhaustive]
35#[derive(Default, Clone, Debug)]
36pub struct Accept {
37 pub subprotocol: Option<Cow<'static, str>>,
38 pub headers: http::HeaderMap,
39}
40
41#[non_exhaustive]
42#[derive(Default, Clone, Debug)]
43pub struct TextFrame {
44 pub data: String,
45}
46
47#[non_exhaustive]
48#[derive(Default, Clone, Debug)]
49pub struct BinaryFrame {
50 pub data: Bytes,
51}
52
53#[non_exhaustive]
54#[derive(Clone, Debug)]
55pub struct Disconnect {
56 pub code: u16,
57}
58
59impl Default for Disconnect {
60 fn default() -> Self {
61 Self { code: 1005 }
62 }
63}
64
65#[non_exhaustive]
66#[derive(Clone, Debug)]
67pub struct Close {
68 pub code: u16,
69 pub reason: Option<Cow<'static, str>>,
70}
71
72impl Default for Close {
73 fn default() -> Self {
74 Self {
75 code: 1000,
76 reason: Default::default(),
77 }
78 }
79}