-
Notifications
You must be signed in to change notification settings - Fork 147
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
Conversation
There was a problem hiding this 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
crates/net/src/websocket/futures.rs
Outdated
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()))) }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that sounds good
Thanks a lot for the PR 🎉 |
The websocket feature requires `Serde` and `wasm-bindgen/serde-serialize` since #222
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 withWebSocket::open_with_protocols