Skip to content

Commit

Permalink
update tokio-tungstenite
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 20, 2023
1 parent 623fcbe commit 1e7feb5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 37 deletions.
40 changes: 13 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ thiserror = { workspace = true }
rand = "0.8"
futures-util = "0.3"
tokio = { version = "1", features = [ "net", "sync" ] }
tokio-tungstenite = { version = "0.19" }
tokio-tungstenite = { version = "0.20" }

[features]
native-tls = [ "tokio-tungstenite/native-tls" ]
Expand Down
2 changes: 1 addition & 1 deletion plugins/websocket/examples/svelte-app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tauri = { workspace = true }
tokio = { version = "1", features = ["net"] }
futures-util = "0.3"
tauri-plugin-websocket = { path = "../../../" }
tokio-tungstenite = "0.19"
tokio-tungstenite = "0.20"

[build-dependencies]
tauri-build = { workspace = true }
Expand Down
13 changes: 11 additions & 2 deletions plugins/websocket/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

import { invoke, Channel } from "@tauri-apps/api/primitives";

export interface ConnectionConfig {
writeBufferSize?: number;
maxWriteBufferSize?: number;
maxMessageSize?: number;
maxFrameSize?: number;
acceptUnmaskedFrames?: boolean;
headers?: HeadersInit;
}

export interface MessageKind<T, D> {
type: T;
data: D;
Expand All @@ -30,7 +39,7 @@ export default class WebSocket {
this.listeners = listeners;
}

static async connect(url: string, options?: unknown): Promise<WebSocket> {
static async connect(url: string, config?: ConnectionConfig): Promise<WebSocket> {
const listeners: Array<(arg: Message) => void> = [];

const onMessage = new Channel<Message>();
Expand All @@ -41,7 +50,7 @@ export default class WebSocket {
return await invoke<number>("plugin:websocket|connect", {
url,
onMessage,
options,
config,
}).then((id) => new WebSocket(id, listeners));
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/websocket/src/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions plugins/websocket/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ impl Serialize for Error {
#[derive(Default)]
struct ConnectionManager(Mutex<HashMap<Id, WebSocketWriter>>);

#[derive(Default, Deserialize)]
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConnectionConfig {
pub max_send_queue: Option<usize>,
pub write_buffer_size: Option<usize>,
pub max_write_buffer_size: Option<usize>,
pub max_message_size: Option<usize>,
pub max_frame_size: Option<usize>,
#[serde(default)]
pub accept_unmasked_frames: bool,
pub headers: Option<Vec<(String, String)>>,
}

impl From<ConnectionConfig> for WebSocketConfig {
fn from(config: ConnectionConfig) -> Self {
// Disabling the warning on max_send_queue which we don't use anymore since it was deprecated.
#[allow(deprecated)]
Self {
max_send_queue: config.max_send_queue,
max_send_queue: None,
write_buffer_size: config.write_buffer_size.unwrap_or(128 * 1024),
max_write_buffer_size: config.max_write_buffer_size.unwrap_or(usize::MAX),
// This may be harmful since if it's not provided from js we're overwriting the default value with None, meaning no size limit.
max_message_size: config.max_message_size,
// This may be harmful since if it's not provided from js we're overwriting the default value with None, meaning no size limit.
max_frame_size: config.max_frame_size,
accept_unmasked_frames: config.accept_unmasked_frames,
}
Expand Down Expand Up @@ -96,10 +105,10 @@ async fn connect<R: Runtime>(
window: Window<R>,
url: String,
on_message: Channel,
options: Option<ConnectionConfig>,
config: Option<ConnectionConfig>,
) -> Result<Id> {
let id = rand::random();
let (ws_stream, _) = connect_async_with_config(url, options.map(Into::into), false).await?;
let (ws_stream, _) = connect_async_with_config(url, config.map(Into::into), false).await?;

tauri::async_runtime::spawn(async move {
let (write, read) = ws_stream.split();
Expand Down

0 comments on commit 1e7feb5

Please sign in to comment.