|
|
@ -1,14 +1,11 @@
|
|
|
|
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
|
|
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
export interface ConnectionConfig {
|
|
|
|
interface Window {
|
|
|
|
writeBufferSize?: number;
|
|
|
|
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
|
|
|
maxWriteBufferSize?: number;
|
|
|
|
__TAURI__: {
|
|
|
|
maxMessageSize?: number;
|
|
|
|
transformCallback: <T>(cb: (payload: T) => void) => number;
|
|
|
|
maxFrameSize?: number;
|
|
|
|
};
|
|
|
|
acceptUnmaskedFrames?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface MessageKind<T, D> {
|
|
|
|
export interface MessageKind<T, D> {
|
|
|
@ -37,19 +34,20 @@ 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?: ConnectionConfig,
|
|
|
|
|
|
|
|
): 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));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return await window
|
|
|
|
return await invoke<number>("plugin:websocket|connect", {
|
|
|
|
.__TAURI_INVOKE__<number>("plugin:websocket|connect", {
|
|
|
|
url,
|
|
|
|
url,
|
|
|
|
callbackFunction: transformCallback(handler),
|
|
|
|
callbackFunction: window.__TAURI__.transformCallback(handler),
|
|
|
|
config,
|
|
|
|
options,
|
|
|
|
}).then((id) => new WebSocket(id, listeners));
|
|
|
|
})
|
|
|
|
|
|
|
|
.then((id) => new WebSocket(id, listeners));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addListener(cb: (arg: Message) => void): void {
|
|
|
|
addListener(cb: (arg: Message) => void): void {
|
|
|
@ -69,7 +67,7 @@ export default class WebSocket {
|
|
|
|
"invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array",
|
|
|
|
"invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array",
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await window.__TAURI_INVOKE__("plugin:websocket|send", {
|
|
|
|
return await invoke("plugin:websocket|send", {
|
|
|
|
id: this.id,
|
|
|
|
id: this.id,
|
|
|
|
message: m,
|
|
|
|
message: m,
|
|
|
|
});
|
|
|
|
});
|
|
|
|