1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::global::EventTarget;
use js_sys::Uint8Array;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {

    #[wasm_bindgen(extends = js_sys::Object, js_name = WebSocketPair)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    #[doc = "The `WebSocketPair` dictionary."]
    pub type WebSocketPair;

    #[wasm_bindgen(constructor, js_class = WebSocketPair)]
    pub fn new() -> WebSocketPair;
}

impl WebSocketPair {
    pub fn client(&mut self) -> Result<WebSocket, JsValue> {
        let value = ::js_sys::Reflect::get(self.as_ref(), &JsValue::from("0"))?;
        Ok(WebSocket::from(value))
    }

    pub fn server(&mut self) -> Result<WebSocket, JsValue> {
        let value = ::js_sys::Reflect::get(self.as_ref(), &JsValue::from("1"))?;
        Ok(WebSocket::from(value))
    }
}

#[wasm_bindgen]
extern "C" {

    #[wasm_bindgen(extends = EventTarget, extends = js_sys::Object, js_name = WebSocket)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    #[doc = "The `WebSocket` class."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)"]
    pub type WebSocket;

    #[wasm_bindgen(catch, structural, method, js_class = "WebSocket", js_name = accept)]
    #[doc = "Accepts the server side of the WebSocket."]
    #[doc = ""]
    #[doc = "[CF Documentation](https://developers.cloudflare.com/workers/runtime-apis/websockets#accept)"]
    pub fn accept(this: &WebSocket) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
    #[doc = "The `close()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
    pub fn close(this: &WebSocket) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
    #[doc = "The `close()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
    pub fn close_with_code(this: &WebSocket, code: u16) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
    #[doc = "The `close()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
    pub fn close_with_code_and_reason(
        this: &WebSocket,
        code: u16,
        reason: &str,
    ) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = send)]
    #[doc = "The `send()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
    pub fn send_with_str(this: &WebSocket, data: &str) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = send)]
    #[doc = "The `send()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
    pub fn send_with_u8_array(this: &WebSocket, data: Uint8Array) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = addEventListener)]
    #[doc = "The `addEventListener()` method."]
    #[doc = ""]
    #[doc = "[CF Documentation](https://developers.cloudflare.com/workers/runtime-apis/websockets#addeventlistener)"]
    pub fn add_event_listener(
        this: &WebSocket,
        r#type: JsValue,
        value: Option<&::js_sys::Function>,
    ) -> Result<(), JsValue>;

    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = removeEventListener)]
    #[doc = "The `removeEventListener()` method."]
    #[doc = ""]
    #[doc = "[MDN Documentation](hhttps://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener)"]
    pub fn remove_event_listener(
        this: &WebSocket,
        r#type: JsValue,
        value: Option<&::js_sys::Function>,
    ) -> Result<(), JsValue>;
}