Initializes the web socket connection. Connect to a given url, asynchronously. The connection will be run in a separate thread managed by the websocket provider. Callbacks will be called **on the thread of the connection**, so be sure to ExecuteOnMainThread any long-running or gui operations in the callbacks. If the connection succeeds, [WebsocketClientCallback::connected] will be called. On no
(
&self,
host: &str,
headers: I,
callbacks: &mut C,
)
| 70 | /// * `headers` - HTTP header keys and values |
| 71 | /// * `callback` - Callbacks for various websocket events |
| 72 | pub fn initialize_connection<I, K, V, C>( |
| 73 | &self, |
| 74 | host: &str, |
| 75 | headers: I, |
| 76 | callbacks: &mut C, |
| 77 | ) -> bool |
| 78 | where |
| 79 | I: IntoIterator<Item = (K, V)>, |
| 80 | K: BnStrCompatible, |
| 81 | V: BnStrCompatible, |
| 82 | C: WebsocketClientCallback, |
| 83 | { |
| 84 | let url = host.into_bytes_with_nul(); |
| 85 | let (header_keys, header_values): (Vec<K::Result>, Vec<V::Result>) = headers |
| 86 | .into_iter() |
| 87 | .map(|(k, v)| (k.into_bytes_with_nul(), v.into_bytes_with_nul())) |
| 88 | .unzip(); |
| 89 | let header_keys: Vec<*const c_char> = header_keys |
| 90 | .iter() |
| 91 | .map(|k| k.as_ref().as_ptr() as *const c_char) |
| 92 | .collect(); |
| 93 | let header_values: Vec<*const c_char> = header_values |
| 94 | .iter() |
| 95 | .map(|v| v.as_ref().as_ptr() as *const c_char) |
| 96 | .collect(); |
| 97 | // SAFETY: This context will only be live for the duration of BNConnectWebsocketClient |
| 98 | // SAFETY: Any subsequent call to BNConnectWebsocketClient will write over the context. |
| 99 | let mut output_callbacks = BNWebsocketClientOutputCallbacks { |
| 100 | context: callbacks as *mut C as *mut c_void, |
| 101 | connectedCallback: Some(cb_connected::<C>), |
| 102 | disconnectedCallback: Some(cb_disconnected::<C>), |
| 103 | errorCallback: Some(cb_error::<C>), |
| 104 | readCallback: Some(cb_read::<C>), |
| 105 | }; |
| 106 | unsafe { |
| 107 | BNConnectWebsocketClient( |
| 108 | self.handle.as_ptr(), |
| 109 | url.as_ptr() as *const c_char, |
| 110 | header_keys.len().try_into().unwrap(), |
| 111 | header_keys.as_ptr(), |
| 112 | header_values.as_ptr(), |
| 113 | &mut output_callbacks, |
| 114 | ) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Call the connect callback function, forward the callback returned value |
| 119 | pub fn notify_connected(&self) -> bool { |