1#![allow(unused_imports)]
2#![allow(clippy::all)]
3use super::config::*;
4use super::*;
5use std::result::Result;
6use wasm_bindgen::prelude::*;
7use workflow_core::runtime::*;
8use workflow_wasm::options::OptionsTrait;
9
10#[wasm_bindgen]
11extern "C" {
12 # [wasm_bindgen (extends = :: web_sys :: EventTarget , extends = :: js_sys :: Object , js_name = WebSocket , typescript_type = "WebSocket")]
13 #[derive(Debug, Clone, PartialEq, Eq)]
14 #[doc = "The `WebSocket` class."]
15 #[doc = ""]
16 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)"]
17 #[doc = ""]
18 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
19 pub type WebSocket;
20 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = url)]
21 #[doc = "Getter for the `url` field of this object."]
22 #[doc = ""]
23 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/url)"]
24 #[doc = ""]
25 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
26 pub fn url(this: &WebSocket) -> String;
27 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = readyState)]
28 #[doc = "Getter for the `readyState` field of this object."]
29 #[doc = ""]
30 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState)"]
31 #[doc = ""]
32 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
33 pub fn ready_state(this: &WebSocket) -> u16;
34 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = bufferedAmount)]
35 #[doc = "Getter for the `bufferedAmount` field of this object."]
36 #[doc = ""]
37 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount)"]
38 #[doc = ""]
39 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
40 pub fn buffered_amount(this: &WebSocket) -> u32;
41 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = onopen)]
42 #[doc = "Getter for the `onopen` field of this object."]
43 #[doc = ""]
44 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onopen)"]
45 #[doc = ""]
46 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
47 pub fn onopen(this: &WebSocket) -> Option<::js_sys::Function>;
48 # [wasm_bindgen (structural , method , setter , js_class = "WebSocket" , js_name = onopen)]
49 #[doc = "Setter for the `onopen` field of this object."]
50 #[doc = ""]
51 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onopen)"]
52 #[doc = ""]
53 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
54 pub fn set_onopen(this: &WebSocket, value: Option<&::js_sys::Function>);
55 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = onerror)]
56 #[doc = "Getter for the `onerror` field of this object."]
57 #[doc = ""]
58 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onerror)"]
59 #[doc = ""]
60 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
61 pub fn onerror(this: &WebSocket) -> Option<::js_sys::Function>;
62 # [wasm_bindgen (structural , method , setter , js_class = "WebSocket" , js_name = onerror)]
63 #[doc = "Setter for the `onerror` field of this object."]
64 #[doc = ""]
65 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onerror)"]
66 #[doc = ""]
67 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
68 pub fn set_onerror(this: &WebSocket, value: Option<&::js_sys::Function>);
69 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = onclose)]
70 #[doc = "Getter for the `onclose` field of this object."]
71 #[doc = ""]
72 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onclose)"]
73 #[doc = ""]
74 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
75 pub fn onclose(this: &WebSocket) -> Option<::js_sys::Function>;
76 # [wasm_bindgen (structural , method , setter , js_class = "WebSocket" , js_name = onclose)]
77 #[doc = "Setter for the `onclose` field of this object."]
78 #[doc = ""]
79 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onclose)"]
80 #[doc = ""]
81 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
82 pub fn set_onclose(this: &WebSocket, value: Option<&::js_sys::Function>);
83 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = extensions)]
84 #[doc = "Getter for the `extensions` field of this object."]
85 #[doc = ""]
86 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/extensions)"]
87 #[doc = ""]
88 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
89 pub fn extensions(this: &WebSocket) -> String;
90 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = protocol)]
91 #[doc = "Getter for the `protocol` field of this object."]
92 #[doc = ""]
93 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/protocol)"]
94 #[doc = ""]
95 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
96 pub fn protocol(this: &WebSocket) -> String;
97 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = onmessage)]
98 #[doc = "Getter for the `onmessage` field of this object."]
99 #[doc = ""]
100 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onmessage)"]
101 #[doc = ""]
102 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
103 pub fn onmessage(this: &WebSocket) -> Option<::js_sys::Function>;
104 # [wasm_bindgen (structural , method , setter , js_class = "WebSocket" , js_name = onmessage)]
105 #[doc = "Setter for the `onmessage` field of this object."]
106 #[doc = ""]
107 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/onmessage)"]
108 #[doc = ""]
109 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
110 pub fn set_onmessage(this: &WebSocket, value: Option<&::js_sys::Function>);
111 # [wasm_bindgen (structural , method , getter , js_class = "WebSocket" , js_name = binaryType)]
113 #[doc = "Getter for the `binaryType` field of this object."]
114 #[doc = ""]
115 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType)"]
116 #[doc = ""]
117 #[doc = "*This API requires the following crate features to be activated: `BinaryType`, `WebSocket`*"]
118 pub fn binary_type(this: &WebSocket) -> web_sys::BinaryType;
119 # [wasm_bindgen (method , setter , js_class = "WebSocket" , js_name = binaryType)]
121 #[doc = "Setter for the `binaryType` field of this object."]
122 #[doc = ""]
123 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType)"]
124 #[doc = ""]
125 #[doc = "*This API requires the following crate features to be activated: `BinaryType`, `WebSocket`*"]
126 pub fn set_binary_type(this: &WebSocket, value: web_sys::BinaryType);
127 #[wasm_bindgen(catch, constructor, js_class = "WebSocket")]
128 #[doc = "The `new WebSocket(..)` constructor, creating a new instance of `WebSocket`."]
129 #[doc = ""]
130 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket)"]
131 #[doc = ""]
132 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
133 pub fn new(url: &str) -> Result<WebSocket, JsValue>;
134 #[wasm_bindgen(catch, constructor, js_class = "WebSocket")]
135 #[doc = "The `new WebSocket(..)` constructor, creating a new instance of `WebSocket`."]
136 #[doc = ""]
137 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket)"]
138 #[doc = ""]
139 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
140 pub fn new_with_str(url: &str, protocols: &str) -> Result<WebSocket, JsValue>;
141 #[wasm_bindgen(catch, constructor, js_class = "WebSocket")]
142 #[doc = "The `new WebSocket(..)` constructor, creating a new instance of `WebSocket`."]
143 #[doc = ""]
144 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket)"]
145 #[doc = ""]
146 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
147 pub fn new_with_str_sequence(
148 url: &str,
149 protocols: &::wasm_bindgen::JsValue,
150 ) -> Result<WebSocket, JsValue>;
151 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = close)]
152 #[doc = "The `close()` method."]
153 #[doc = ""]
154 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
155 #[doc = ""]
156 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
157 pub fn close(this: &WebSocket) -> Result<(), JsValue>;
158 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = close)]
159 #[doc = "The `close()` method."]
160 #[doc = ""]
161 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
162 #[doc = ""]
163 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
164 pub fn close_with_code(this: &WebSocket, code: u16) -> Result<(), JsValue>;
165 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = close)]
166 #[doc = "The `close()` method."]
167 #[doc = ""]
168 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close)"]
169 #[doc = ""]
170 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
171 pub fn close_with_code_and_reason(
172 this: &WebSocket,
173 code: u16,
174 reason: &str,
175 ) -> Result<(), JsValue>;
176 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = send)]
177 #[doc = "The `send()` method."]
178 #[doc = ""]
179 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
180 #[doc = ""]
181 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
182 pub fn send_with_str(this: &WebSocket, data: &str) -> Result<(), JsValue>;
183 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = send)]
185 #[doc = "The `send()` method."]
186 #[doc = ""]
187 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
188 #[doc = ""]
189 #[doc = "*This API requires the following crate features to be activated: `Blob`, `WebSocket`*"]
190 pub fn send_with_blob(this: &WebSocket, data: &web_sys::Blob) -> Result<(), JsValue>;
191 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = send)]
192 #[doc = "The `send()` method."]
193 #[doc = ""]
194 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
195 #[doc = ""]
196 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
197 pub fn send_with_array_buffer(
198 this: &WebSocket,
199 data: &::js_sys::ArrayBuffer,
200 ) -> Result<(), JsValue>;
201 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = send)]
202 #[doc = "The `send()` method."]
203 #[doc = ""]
204 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
205 #[doc = ""]
206 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
207 pub fn send_with_array_buffer_view(
208 this: &WebSocket,
209 data: &::js_sys::Object,
210 ) -> Result<(), JsValue>;
211 # [wasm_bindgen (catch , method , structural , js_class = "WebSocket" , js_name = send)]
212 #[doc = "The `send()` method."]
213 #[doc = ""]
214 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send)"]
215 #[doc = ""]
216 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
217 pub fn send_with_u8_array(this: &WebSocket, data: &[u8]) -> Result<(), JsValue>;
218
219 #[wasm_bindgen(catch, constructor, js_class = "WebSocket")]
220 #[doc = "The `new WebSocket(..)` constructor, creating a new instance of `WebSocket`."]
221 #[doc = ""]
222 #[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket)"]
223 #[doc = ""]
224 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
225 pub fn new_with_nodejs_config_impl(
226 url: &str,
227 protocols: JsValue,
228 origin: JsValue,
229 headers: JsValue,
230 request_options: JsValue,
231 client_config: JsValue,
232 ) -> std::result::Result<WebSocket, JsValue>;
233
234}
235
236impl WebSocket {
237 #[doc = "The `WebSocket.CONNECTING` const."]
238 #[doc = ""]
239 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
240 pub const CONNECTING: u16 = 0i64 as u16;
241 #[doc = "The `WebSocket.OPEN` const."]
242 #[doc = ""]
243 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
244 pub const OPEN: u16 = 1u64 as u16;
245 #[doc = "The `WebSocket.CLOSING` const."]
246 #[doc = ""]
247 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
248 pub const CLOSING: u16 = 2u64 as u16;
249 #[doc = "The `WebSocket.CLOSED` const."]
250 #[doc = ""]
251 #[doc = "*This API requires the following crate features to be activated: `WebSocket`*"]
252 pub const CLOSED: u16 = 3u64 as u16;
253
254 pub fn new_with_config(
255 url: &str,
256 config: &WebSocketConfig,
257 ) -> super::result::Result<WebSocket> {
258 if is_node() {
259 let WebSocketNodeJsConfig {
260 protocols,
261 origin,
262 headers,
263 request_options,
264 client_config,
265 } = WebSocketNodeJsConfig::try_from(config)?;
266
267 Ok(Self::new_with_nodejs_config_impl(
268 url,
269 protocols,
270 origin,
271 headers,
272 request_options,
273 client_config,
274 )?)
275 } else {
276 Ok(Self::new(url)?)
277 }
278 }
279
280 pub fn close_if_open(&self) -> super::result::Result<()> {
281 let ready_state = self.ready_state();
282 if ready_state != Self::CLOSED && ready_state != Self::CLOSING {
283 self.close()?;
284 }
285 Ok(())
286 }
287}