[WIP]
This commit is contained in:
parent
3f65aa92bd
commit
8194fff3b6
4 changed files with 93 additions and 66 deletions
|
|
@ -6,13 +6,13 @@ import init, {
|
|||
ed25519_generate,
|
||||
keyring_from_ed25519,
|
||||
build_demo_message,
|
||||
} from 'mtp-wasm';
|
||||
} from "mtp-wasm";
|
||||
|
||||
const STATUS = document.getElementById('status')!;
|
||||
const STORAGE_KEY = 'mtp-web-client-keys';
|
||||
const STATUS = document.getElementById("status")!;
|
||||
const STORAGE_KEY = "mtp-web-client-keys";
|
||||
|
||||
function log(msg: string, cls = '') {
|
||||
const line = document.createElement('div');
|
||||
function log(msg: string, cls = "") {
|
||||
const line = document.createElement("div");
|
||||
line.textContent = msg;
|
||||
if (cls) line.className = cls;
|
||||
STATUS.appendChild(line);
|
||||
|
|
@ -37,19 +37,23 @@ function loadKeys(): { clientId: bigint; keyringBytes: Uint8Array } | null {
|
|||
}
|
||||
|
||||
async function initWasm() {
|
||||
log('Loading WASM module...');
|
||||
log("Loading WASM module...");
|
||||
await init();
|
||||
log(`WASM loaded. WebTransport supported: ${WasmClient.is_supported()}`);
|
||||
}
|
||||
|
||||
function createClient(): WasmClient {
|
||||
return new WasmClient(
|
||||
(state: number) => log(`[state] ${ConnectionState[state] ?? state}`, 'state'),
|
||||
(state: number) =>
|
||||
log(`[state] ${ConnectionState[state] ?? state}`, "state"),
|
||||
(data: Uint8Array) => {
|
||||
const decoder = new TextDecoder();
|
||||
log(`[message] ${data.length} bytes: ${decoder.decode(data)}`, 'received');
|
||||
log(
|
||||
`[message] ${data.length} bytes: ${decoder.decode(data)}`,
|
||||
"received",
|
||||
);
|
||||
},
|
||||
(err: any) => log(`[error] ${err}`, 'error'),
|
||||
(err: any) => log(`[error] ${err}`, "error"),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -65,11 +69,11 @@ async function run() {
|
|||
await initWasm();
|
||||
|
||||
if (!WasmClient.is_supported()) {
|
||||
log('WebTransport is not supported in this browser.', 'error');
|
||||
log("WebTransport is not supported in this browser.", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
const serverUrl = 'https://127.0.0.1:8080';
|
||||
const serverUrl = "https://127.0.0.1:8080";
|
||||
const saved = loadKeys();
|
||||
|
||||
const client = createClient();
|
||||
|
|
@ -81,30 +85,35 @@ async function run() {
|
|||
if (saved) {
|
||||
log(`Found saved client keys (ID: ${saved.clientId})`);
|
||||
const hostPk = new Uint8Array(0);
|
||||
clientId = await client.auth_connect(config, hostPk, saved.keyringBytes, saved.clientId);
|
||||
clientId = await client.auth_connect(
|
||||
config,
|
||||
hostPk,
|
||||
saved.keyringBytes,
|
||||
saved.clientId,
|
||||
);
|
||||
log(`Authenticated as client ${clientId}`);
|
||||
keyringBytes = saved.keyringBytes;
|
||||
} else {
|
||||
log('No saved keys — registering new client...');
|
||||
log("No saved keys: registering new client...");
|
||||
const hostPk = new Uint8Array(0);
|
||||
keyringBytes = generateKeyringBytes();
|
||||
clientId = await client.auth_register(config, hostPk, keyringBytes);
|
||||
log(`Registered with ID: ${clientId}`);
|
||||
saveKeys(clientId, keyringBytes);
|
||||
log('Saved client keys to localStorage');
|
||||
log("Saved client keys to localStorage");
|
||||
}
|
||||
|
||||
config.free();
|
||||
|
||||
log('\nSending demo message...');
|
||||
log("\nSending demo message...");
|
||||
const frame = build_demo_message(clientId, keyringBytes);
|
||||
await client.send(frame);
|
||||
log(`Sent ${frame.length} bytes`);
|
||||
|
||||
log('\nClient running. Waiting for incoming messages...');
|
||||
log("\nClient running. Waiting for incoming messages...");
|
||||
}
|
||||
|
||||
run().catch((e) => {
|
||||
log(`Fatal error: ${e}`, 'error');
|
||||
log(`Fatal error: ${e}`, "error");
|
||||
console.error(e);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue