Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for specifying Websocket Protocols #222

Merged
merged 2 commits into from
Jun 24, 2022
Merged

Added support for specifying Websocket Protocols #222

merged 2 commits into from
Jun 24, 2022

Conversation

sammhicks
Copy link
Contributor

Currently, WebSockets can only be created without specifying the protocol. I've added support for either specifying a single protocol using WebSocket::open_with_protocol or a list of protocols with WebSocket::open_with_protocols

Copy link
Collaborator

@ranile ranile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it took me a little bit to get to this

Self::setup(web_sys::WebSocket::new_with_str_sequence(
url,
&JsValue::from_serde(protocols).map_err(|err| {
js_sys::Error::new(&format!("Failed to convert protocols to JSON: {}", err))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't converting these to JSON, are we? This error message seems misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at JsValue::from_serde, it seems that we serialize to a JSON string and then parse as a JS value. I suppose the error message is too literal. Would it be better if changed the error message to "Failed to convert protocols to Javascript value: {}" ?

    #[cfg(feature = "serde-serialize")]
    pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
    where
        T: serde::ser::Serialize + ?Sized,
    {
        let s = serde_json::to_string(t)?;
        unsafe { Ok(JsValue::_new(__wbindgen_json_parse(s.as_ptr(), s.len()))) }
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds good

crates/net/src/websocket/futures.rs Show resolved Hide resolved
@ranile ranile merged commit ea3cd31 into rustwasm:master Jun 24, 2022
@ranile
Copy link
Collaborator

ranile commented Jun 24, 2022

Thanks a lot for the PR 🎉

ranile pushed a commit that referenced this pull request Jul 1, 2022
The websocket feature requires `Serde` and `wasm-bindgen/serde-serialize` since #222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants