From 4cdb14fdc9a5903aa03e1b7a6433cff3ea5ab722 Mon Sep 17 00:00:00 2001 From: benStre Date: Thu, 21 Mar 2024 22:42:38 +0100 Subject: [PATCH] refactoring --- .../window-interface.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/network/communication-interfaces/window-interface.ts b/network/communication-interfaces/window-interface.ts index 3d4014e..9a31c0c 100644 --- a/network/communication-interfaces/window-interface.ts +++ b/network/communication-interfaces/window-interface.ts @@ -2,6 +2,7 @@ import { Runtime } from "../../runtime/runtime.ts"; import { Endpoint, Target } from "../../types/addressing.ts"; import { CommunicationInterface, CommunicationInterfaceSocket, InterfaceDirection, InterfaceProperties } from "../communication-interface.ts"; import { communicationHub } from "../communication-hub.ts"; +import { arrayBufferToBase64, base64ToArrayBuffer } from "../../utils/utils.ts"; export class WindowInterfaceSocket extends CommunicationInterfaceSocket { constructor(public readonly window: Window, public readonly windowOrigin: string, public readonly transmissionMode: "buffer"|"json" = "buffer") { @@ -11,7 +12,7 @@ export class WindowInterfaceSocket extends CommunicationInterfaceSocket { handleReceive = (event: MessageEvent) => { if (event.origin == this.windowOrigin) { if (event.data instanceof ArrayBuffer) this.receive(event.data) - else if (typeof event.data == "string") this.receive(stringToArrayBuffer(event.data)) + else if (typeof event.data == "string") this.receive(base64ToArrayBuffer(event.data)) } } @@ -25,7 +26,7 @@ export class WindowInterfaceSocket extends CommunicationInterfaceSocket { send(dxb: ArrayBuffer) { try { - if (this.transmissionMode == "json") this.window.postMessage(arrayBufferToString(dxb), this.windowOrigin) + if (this.transmissionMode == "json") this.window.postMessage(arrayBufferToBase64(dxb), this.windowOrigin) else this.window.postMessage(dxb, this.windowOrigin) return true; } @@ -243,17 +244,4 @@ export class WindowInterface extends CommunicationInterface { }) } -} - -export function arrayBufferToString(buf: ArrayBuffer) { - return String.fromCharCode.apply(null, new Uint16Array(buf) as unknown as number[]); -} - -export function stringToArrayBuffer(str: string) { - const buf = new ArrayBuffer(str.length*2); // 2 bytes for each char - const bufView = new Uint16Array(buf); - for (let i=0, strLen=str.length; i < strLen; i++) { - bufView[i] = str.charCodeAt(i); - } - return buf; } \ No newline at end of file