mirror of
https://github.com/tauri-apps/tauri-plugin-websocket.git
synced 2026-01-31 00:35:19 +01:00
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/13205128094 Co-authored-by: FabianLars <FabianLars@users.noreply.github.com>
57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
import { invoke, transformCallback } from '@tauri-apps/api/tauri';
|
|
|
|
class WebSocket {
|
|
constructor(id, listeners) {
|
|
this.id = id;
|
|
this.listeners = listeners;
|
|
}
|
|
static async connect(url, config) {
|
|
const listeners = [];
|
|
const handler = (message) => {
|
|
listeners.forEach((l) => l(message));
|
|
};
|
|
if (config === null || config === void 0 ? void 0 : config.headers) {
|
|
config.headers = Array.from(new Headers(config.headers).entries());
|
|
}
|
|
return await invoke("plugin:websocket|connect", {
|
|
url,
|
|
callbackFunction: transformCallback(handler),
|
|
config,
|
|
}).then((id) => new WebSocket(id, listeners));
|
|
}
|
|
addListener(cb) {
|
|
this.listeners.push(cb);
|
|
}
|
|
async send(message) {
|
|
let m;
|
|
if (typeof message === "string") {
|
|
m = { type: "Text", data: message };
|
|
}
|
|
else if (typeof message === "object" && "type" in message) {
|
|
m = message;
|
|
}
|
|
else if (Array.isArray(message)) {
|
|
m = { type: "Binary", data: message };
|
|
}
|
|
else {
|
|
throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");
|
|
}
|
|
return await invoke("plugin:websocket|send", {
|
|
id: this.id,
|
|
message: m,
|
|
});
|
|
}
|
|
async disconnect() {
|
|
return await this.send({
|
|
type: "Close",
|
|
data: {
|
|
code: 1000,
|
|
reason: "Disconnected by client",
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
export { WebSocket as default };
|
|
//# sourceMappingURL=index.mjs.map
|