This commit is contained in:
2026-08-20 15:10:25 +02:00
parent cce9756a1b
commit 8cf69d5b02
9 changed files with 85 additions and 66 deletions
+40 -1
View File
@@ -7,6 +7,7 @@ import { base58 } from "@scure/base";
import axios from "axios";
import { Account, AccountsFile, getAccounts } from "@/lib/accounts";
import { Page } from "@/components/page/PageView";
import { ClientMethod } from "./protocol";
ed.hashes.sha512 = sha512;
@@ -124,7 +125,12 @@ export default class Enclave<P = Page> {
return;
}
this.server.websocket?.send({ method: "Meta", ...account.meta });
if (this.server.websocket) {
this.server.websocket.send({ method: "Meta", ...account.meta });
this.server.websocket.websocket.onmessage = (msg) => {
this.onMessage(JSON.parse(msg.data));
};
}
const metaResponse = await axios.get(
getHTTPUrl(hostname, isSecure, "/meta"),
@@ -140,4 +146,37 @@ export default class Enclave<P = Page> {
this.forceRender();
}
public async onMessage(msg: ClientMethod) {
const server = this.server;
if (!server) {
console.error("Unable to get server for message, MSG:", msg);
return;
}
switch (msg.method) {
case "Initialized":
console.error("Already initialized");
return;
case "Error":
console.error("Server error:", msg.error);
return;
case "Messages":
console.log("msg", msg.messages);
Object.entries(msg.messages).forEach(([channelId, messages]) => {
if (!server.messages[channelId]) {
server.messages[channelId] = [];
}
messages.forEach((message) => {
server.messages[channelId].push(message);
});
});
return;
}
}
}
+12 -3
View File
@@ -1,4 +1,4 @@
import { ClientMeta, MessageData } from "@/lib/types";
import { ClientMeta, MessageData, StoredMessage } from "@/lib/types";
export type ClientMethod =
| {
@@ -11,6 +11,10 @@ export type ClientMethod =
| {
method: "Error";
error: string;
}
| {
method: "Messages";
messages: Record<string, StoredMessage[]>;
};
export type ServerMethod =
@@ -21,6 +25,10 @@ export type ServerMethod =
timestamp: number;
hostname: string;
}
| {
method: "Error";
error: string;
}
| ({
method: "Meta";
} & ClientMeta)
@@ -30,6 +38,7 @@ export type ServerMethod =
data: MessageData;
}
| {
method: "Error";
error: string;
method: "GetMessages";
channel_id: string;
chunk: number;
};
+3 -1
View File
@@ -3,7 +3,7 @@ import * as ed from "@noble/ed25519";
import EnclaveWebSocket from "./ws";
import { sha512 } from "@noble/hashes/sha2.js";
import { getWSUrl } from "@/lib/serverList";
import { ServerMeta } from "@/lib/types";
import { ServerMeta, StoredMessage } from "@/lib/types";
ed.hashes.sha512 = sha512;
@@ -29,10 +29,12 @@ export default class EnclaveServer {
public isSecure: boolean;
public websocket?: EnclaveWebSocket;
public meta?: ServerMeta;
public messages: Record<string, StoredMessage[]>;
public constructor(hostname: string, isSecure: boolean) {
this.hostname = hostname;
this.isSecure = isSecure;
this.messages = {};
}
public disconnect() {
+12 -7
View File
@@ -1,5 +1,6 @@
import Enclave from "@/app/app";
import { ChannelPageProps } from "@/components/page/PageView";
import { useEffect } from "react";
export default function TextChannel({
appRef,
@@ -10,13 +11,17 @@ export default function TextChannel({
if (!channel) return null;
useEffect(() => {
appRef.current?.server?.websocket?.send({
method: "GetMessages",
channel_id: channel.id,
chunk: 0,
});
appRef.current?.forceRender();
}, []);
return (
<div
onClick={() => {
appRef.current?.sendMessage("Hello, World", channel.id);
}}
>
{channel.name}
</div>
<div>{JSON.stringify(appRef.current?.server?.messages[channel.id])}</div>
);
}