Working ws
This commit is contained in:
+18
-1
@@ -1,12 +1,29 @@
|
|||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import reactLogo from "./assets/react.svg";
|
import reactLogo from "./assets/react.svg";
|
||||||
import { invoke } from "@tauri-apps/api/core";
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
import { EnclaveWebSocket } from "./protocol/ws";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [greetMsg, setGreetMsg] = useState("");
|
const [greetMsg, setGreetMsg] = useState("");
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
const ws = new EnclaveWebSocket("ws://localhost:3415");
|
||||||
|
|
||||||
|
ws.send({
|
||||||
|
method: "Initialize",
|
||||||
|
public_key: "",
|
||||||
|
signature: "",
|
||||||
|
timestamp: 0,
|
||||||
|
hostname: "",
|
||||||
|
});
|
||||||
|
|
||||||
|
// console.log(await ws.read());
|
||||||
|
})();
|
||||||
|
}, []);
|
||||||
|
|
||||||
async function greet() {
|
async function greet() {
|
||||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||||
setGreetMsg(await invoke("greet", { name }));
|
setGreetMsg(await invoke("greet", { name }));
|
||||||
|
|||||||
+18
-8
@@ -2,33 +2,43 @@ import { ClientMethod, ServerMethod } from "./protocol";
|
|||||||
|
|
||||||
export class EnclaveWebSocket {
|
export class EnclaveWebSocket {
|
||||||
public websocket: WebSocket;
|
public websocket: WebSocket;
|
||||||
onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
|
onOpenQueue: Array<() => void>;
|
||||||
|
|
||||||
public clientPublicKey: Uint8Array;
|
public clientPublicKey: Uint8Array;
|
||||||
public clientSecretKey: Uint8Array;
|
public clientSecretKey: Uint8Array;
|
||||||
|
|
||||||
public serverPublicKey: Uint8Array;
|
public serverPublicKey?: Uint8Array;
|
||||||
|
|
||||||
public constructor(url: string | URL) {
|
public constructor(url: string | URL) {
|
||||||
this.websocket = new WebSocket(url);
|
|
||||||
this.clientPublicKey = new Uint8Array();
|
this.clientPublicKey = new Uint8Array();
|
||||||
this.clientSecretKey = new Uint8Array();
|
this.clientSecretKey = new Uint8Array();
|
||||||
|
this.onOpenQueue = new Array();
|
||||||
|
|
||||||
this.serverPublicKey = new Uint8Array();
|
this.websocket = new WebSocket(url);
|
||||||
|
this.websocket.onopen = () => {
|
||||||
|
this.onOpenQueue.forEach((fun) => fun());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
this.onmessage = null;
|
public async send(method: ServerMethod) {
|
||||||
|
if (this.websocket.readyState !== WebSocket.OPEN) {
|
||||||
|
return new Promise<void>((ok) => {
|
||||||
|
this.onOpenQueue.push(() => {
|
||||||
|
this.websocket.send(JSON.stringify(method));
|
||||||
|
ok();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public send(method: ServerMethod) {
|
|
||||||
this.websocket.send(JSON.stringify(method));
|
this.websocket.send(JSON.stringify(method));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read(): Promise<ClientMethod> {
|
public async read(): Promise<ClientMethod> {
|
||||||
return new Promise((ok) => {
|
return new Promise((ok) => {
|
||||||
this.onmessage = (msg) => {
|
this.websocket.onmessage = (msg) => {
|
||||||
const data = JSON.parse(msg.data);
|
const data = JSON.parse(msg.data);
|
||||||
ok(data);
|
ok(data);
|
||||||
this.onmessage = null;
|
this.websocket.onmessage = null;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user