Merge branch 'fix-websocket-config' into v2

# Conflicts:
#	plugins/websocket/src/lib.rs
pull/540/head
DreamingCodes 2 years ago
commit 346ff80cc5
No known key found for this signature in database
GPG Key ID: 294D2672488EAE23

@ -37,7 +37,7 @@ export default class WebSocket {
this.listeners = listeners; this.listeners = listeners;
} }
static async connect(url: string, options?: unknown): Promise<WebSocket> { static async connect(url: string, config?: unknown): Promise<WebSocket> {
const listeners: Array<(arg: Message) => void> = []; const listeners: Array<(arg: Message) => void> = [];
const handler = (message: Message): void => { const handler = (message: Message): void => {
listeners.forEach((l) => l(message)); listeners.forEach((l) => l(message));
@ -47,7 +47,7 @@ export default class WebSocket {
.__TAURI_INVOKE__<number>("plugin:websocket|connect", { .__TAURI_INVOKE__<number>("plugin:websocket|connect", {
url, url,
callbackFunction: window.__TAURI__.transformCallback(handler), callbackFunction: window.__TAURI__.transformCallback(handler),
options, config,
}) })
.then((id) => new WebSocket(id, listeners)); .then((id) => new WebSocket(id, listeners));
} }

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_WEBSOCKET__=function(){"use strict";class e{constructor(e,t){this.id=e,this.listeners=t}static async connect(t,n){const i=[];return await window.__TAURI_INVOKE__("plugin:websocket|connect",{url:t,callbackFunction:window.__TAURI__.transformCallback((e=>{i.forEach((t=>t(e)))})),options:n}).then((t=>new e(t,i)))}addListener(e){this.listeners.push(e)}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}return await window.__TAURI_INVOKE__("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){return await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return e}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_WEBSOCKET__})} if("__TAURI__"in window){var __TAURI_WEBSOCKET__=function(){"use strict";class e{constructor(e,t){this.id=e,this.listeners=t}static async connect(t,n){const i=[];return await window.__TAURI_INVOKE__("plugin:websocket|connect",{url:t,callbackFunction:window.__TAURI__.transformCallback((e=>{i.forEach((t=>t(e)))})),config:n}).then((t=>new e(t,i)))}addListener(e){this.listeners.push(e)}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}return await window.__TAURI_INVOKE__("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){return await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return e}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_WEBSOCKET__})}

@ -62,14 +62,14 @@ impl Serialize for Error {
#[derive(Default)] #[derive(Default)]
struct ConnectionManager(Mutex<HashMap<Id, WebSocketWriter>>); struct ConnectionManager(Mutex<HashMap<Id, WebSocketWriter>>);
#[derive(Default, Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ConnectionConfig { pub struct ConnectionConfig {
pub max_send_queue: Option<usize>, pub max_send_queue: Option<usize>,
pub max_message_size: Option<usize>, pub max_message_size: Option<usize>,
pub max_frame_size: Option<usize>, pub max_frame_size: Option<usize>,
pub accept_unmasked_frames: bool, pub accept_unmasked_frames: Option<bool>,
pub headers: Vec<(String, String)>, pub headers: Option<Vec<(String, String)>>,
} }
impl From<ConnectionConfig> for WebSocketConfig { impl From<ConnectionConfig> for WebSocketConfig {
@ -78,7 +78,7 @@ impl From<ConnectionConfig> for WebSocketConfig {
max_send_queue: config.max_send_queue, max_send_queue: config.max_send_queue,
max_message_size: config.max_message_size, max_message_size: config.max_message_size,
max_frame_size: config.max_frame_size, max_frame_size: config.max_frame_size,
accept_unmasked_frames: config.accept_unmasked_frames, accept_unmasked_frames: config.accept_unmasked_frames.unwrap_or_default(),
} }
} }
} }
@ -109,8 +109,9 @@ async fn connect<R: Runtime>(
let id = rand::random(); let id = rand::random();
let mut request = url.into_client_request()?; let mut request = url.into_client_request()?;
if let Some(config) = config.as_ref() { if let Some(ref config) = config {
let config_headers = config.headers.iter().map(|(k, v)| { if let Some(headers) = &config.headers {
let config_headers = headers.iter().map(|(k, v)| {
let header_name = HeaderName::from_str(k.as_str())?; let header_name = HeaderName::from_str(k.as_str())?;
let header_value = HeaderValue::from_str(v.as_str())?; let header_value = HeaderValue::from_str(v.as_str())?;
Ok((header_name, header_value)) Ok((header_name, header_value))
@ -118,6 +119,7 @@ async fn connect<R: Runtime>(
request.headers_mut().extend(config_headers.filter_map(Result::ok)); request.headers_mut().extend(config_headers.filter_map(Result::ok));
} }
}
let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?; let (ws_stream, _) = connect_async_with_config(request, config.map(Into::into), false).await?;

Loading…
Cancel
Save