Message sending
This commit is contained in:
@@ -44,6 +44,31 @@ export default class Enclave<P = Page> {
|
|||||||
return this.accounts?.accounts[this.accounts.activeAccount] || null;
|
return this.accounts?.accounts[this.accounts.activeAccount] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sendMessage(content: string, channelId: string) {
|
||||||
|
const clientSecretKey = this.getClientSecretKey();
|
||||||
|
|
||||||
|
if (!clientSecretKey) {
|
||||||
|
console.error("Acccount not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const timestamp = Date.now();
|
||||||
|
|
||||||
|
const signature = new TextEncoder().encode(
|
||||||
|
`${timestamp}@${this.server?.hostname}@${content}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.server?.websocket?.send({
|
||||||
|
method: "SendMessage",
|
||||||
|
channel_id: channelId,
|
||||||
|
data: {
|
||||||
|
content,
|
||||||
|
timestamp,
|
||||||
|
signature: base58.encode(ed.sign(signature, clientSecretKey)),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public async connectToServer(hostname: string, isSecure: boolean) {
|
public async connectToServer(hostname: string, isSecure: boolean) {
|
||||||
if (this.server) {
|
if (this.server) {
|
||||||
this.server.disconnect();
|
this.server.disconnect();
|
||||||
|
|||||||
@@ -6,6 +6,17 @@ export interface ServerMeta {
|
|||||||
channels: Channel[];
|
channels: Channel[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface MessageData {
|
||||||
|
content: string;
|
||||||
|
timestamp: number;
|
||||||
|
signature: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StoredMessage extends MessageData {
|
||||||
|
id: string;
|
||||||
|
author: string;
|
||||||
|
}
|
||||||
|
|
||||||
export type ClientMethod =
|
export type ClientMethod =
|
||||||
| {
|
| {
|
||||||
method: "Initialized";
|
method: "Initialized";
|
||||||
@@ -30,6 +41,11 @@ export type ServerMethod =
|
|||||||
| {
|
| {
|
||||||
method: "Meta";
|
method: "Meta";
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
method: "SendMessage";
|
||||||
|
channel_id: string;
|
||||||
|
data: MessageData;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
method: "Error";
|
method: "Error";
|
||||||
error: string;
|
error: string;
|
||||||
|
|||||||
@@ -6,5 +6,17 @@ export default function TextChannel({
|
|||||||
}: {
|
}: {
|
||||||
appRef: React.RefObject<Enclave<ChannelPageProps> | null>;
|
appRef: React.RefObject<Enclave<ChannelPageProps> | null>;
|
||||||
}) {
|
}) {
|
||||||
return <div>{appRef.current?.page?.channel.name}</div>;
|
const channel = appRef.current?.page?.channel;
|
||||||
|
|
||||||
|
if (!channel) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
onClick={() => {
|
||||||
|
appRef.current?.sendMessage("Hello, World", channel.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{channel.name}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user