worker_plus_sys/
websocket.rs

1use crate::global::EventTarget;
2use js_sys::Uint8Array;
3
4use wasm_bindgen::prelude::*;
5
6#[wasm_bindgen]
7extern "C" {
8
9    #[wasm_bindgen(extends = js_sys::Object, js_name = WebSocketPair)]
10    #[derive(Debug, Clone, PartialEq, Eq)]
11    #[doc = "The `WebSocketPair` dictionary."]
12    pub type WebSocketPair;
13
14    #[wasm_bindgen(constructor, js_class = WebSocketPair)]
15    pub fn new() -> WebSocketPair;
16}
17
18impl WebSocketPair {
19    pub fn client(&mut self) -> Result<WebSocket, JsValue> {
20        let value = ::js_sys::Reflect::get(self.as_ref(), &JsValue::from("0"))?;
21        Ok(WebSocket::from(value))
22    }
23
24    pub fn server(&mut self) -> Result<WebSocket, JsValue> {
25        let value = ::js_sys::Reflect::get(self.as_ref(), &JsValue::from("1"))?;
26        Ok(WebSocket::from(value))
27    }
28}
29
30#[wasm_bindgen]
31extern "C" {
32
33    #[wasm_bindgen(extends = EventTarget, extends = js_sys::Object, js_name = WebSocket)]
34    #[derive(Debug, Clone, PartialEq, Eq)]
35    #[doc = "The `WebSocket` class."]
36    #[doc = ""]
37    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)"]
38    pub type WebSocket;
39
40    #[wasm_bindgen(catch, structural, method, js_class = "WebSocket", js_name = accept)]
41    #[doc = "Accepts the server side of the WebSocket."]
42    #[doc = ""]
43    #[doc = "[CF Documentation](https://developers.cloudflare.com/workers/runtime-apis/websockets#accept)"]
44    pub fn accept(this: &WebSocket) -> Result<(), JsValue>;
45
46    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
47    #[doc = "The `close()` method."]
48    #[doc = ""]
49    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
50    pub fn close(this: &WebSocket) -> Result<(), JsValue>;
51
52    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
53    #[doc = "The `close()` method."]
54    #[doc = ""]
55    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
56    pub fn close_with_code(this: &WebSocket, code: u16) -> Result<(), JsValue>;
57
58    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = close)]
59    #[doc = "The `close()` method."]
60    #[doc = ""]
61    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
62    pub fn close_with_code_and_reason(
63        this: &WebSocket,
64        code: u16,
65        reason: &str,
66    ) -> Result<(), JsValue>;
67
68    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = send)]
69    #[doc = "The `send()` method."]
70    #[doc = ""]
71    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
72    pub fn send_with_str(this: &WebSocket, data: &str) -> Result<(), JsValue>;
73
74    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = send)]
75    #[doc = "The `send()` method."]
76    #[doc = ""]
77    #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
78    pub fn send_with_u8_array(this: &WebSocket, data: Uint8Array) -> Result<(), JsValue>;
79
80    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = addEventListener)]
81    #[doc = "The `addEventListener()` method."]
82    #[doc = ""]
83    #[doc = "[CF Documentation](https://developers.cloudflare.com/workers/runtime-apis/websockets#addeventlistener)"]
84    pub fn add_event_listener(
85        this: &WebSocket,
86        r#type: JsValue,
87        value: Option<&::js_sys::Function>,
88    ) -> Result<(), JsValue>;
89
90    #[wasm_bindgen(catch, method, structural, js_class = "WebSocket", js_name = removeEventListener)]
91    #[doc = "The `removeEventListener()` method."]
92    #[doc = ""]
93    #[doc = "[MDN Documentation](hhttps://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener)"]
94    pub fn remove_event_listener(
95        this: &WebSocket,
96        r#type: JsValue,
97        value: Option<&::js_sys::Function>,
98    ) -> Result<(), JsValue>;
99}