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
use wasm_bindgen::prelude::*;

mod glue {
    use super::*;

    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen]
        pub type WebSocket;

        #[wasm_bindgen(method, catch)]
        pub fn accept(this: &WebSocket) -> Result<(), JsValue>;
    }
}

pub trait WebSocketExt {
    /// Accepts the server side of the WebSocket.
    ///
    /// [CF Documentation](https://developers.cloudflare.com/workers/runtime-apis/websockets#accept)
    fn accept(&self) -> Result<(), JsValue>;
}

impl WebSocketExt for web_sys::WebSocket {
    fn accept(&self) -> Result<(), JsValue> {
        self.unchecked_ref::<glue::WebSocket>().accept()
    }
}