2024-07-06 14:11:48 +02:00
|
|
|
export interface Message {
|
2026-10-12 20:38:04 +02:00
|
|
|
uuid?: string; // to be assigned by
|
|
|
|
|
|
2024-07-08 21:13:56 +02:00
|
|
|
// connecting to server
|
|
|
|
|
requestingServerConnection?: boolean;
|
|
|
|
|
|
|
|
|
|
// mailbox
|
|
|
|
|
requestingMailboxSetup?: boolean;
|
|
|
|
|
requestedMailbox?: string;
|
2024-07-08 23:34:16 +02:00
|
|
|
deletingMailbox?: string;
|
|
|
|
|
assignedMailboxId?: string; // sent by server
|
|
|
|
|
connectedMailboxId?: string; // sent by server
|
2024-07-09 00:30:57 +02:00
|
|
|
deletedMailbox?: string; // sent by server
|
2024-07-08 21:13:56 +02:00
|
|
|
|
2024-07-06 14:11:48 +02:00
|
|
|
// subscribing to channel
|
|
|
|
|
subscribeChannel?: string;
|
|
|
|
|
unsubscribeChannel?: string;
|
2024-07-09 00:30:57 +02:00
|
|
|
subscribed?: boolean; // sent by server
|
2024-07-06 14:11:48 +02:00
|
|
|
|
2026-10-12 20:38:04 +02:00
|
|
|
// sending message
|
2024-07-06 14:11:48 +02:00
|
|
|
messageChannel?: string;
|
|
|
|
|
messageBody?: string;
|
2026-10-12 20:38:04 +02:00
|
|
|
|
|
|
|
|
// confirming message
|
|
|
|
|
confirmingMessageReceived?: boolean;
|
2024-07-06 14:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class UDNFrontend {
|
2024-07-08 21:13:56 +02:00
|
|
|
ws: WebSocket | undefined;
|
2024-07-06 14:11:48 +02:00
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// HANDLERS
|
2026-10-12 20:38:04 +02:00
|
|
|
private connectionHandler = () => { };
|
|
|
|
|
private disconnectionHandler = () => { };
|
|
|
|
|
private messageHandler = (data: Message) => { };
|
|
|
|
|
private mailboxHandler = (mailboxId: string) => { };
|
|
|
|
|
private mailboxConnectionHandler = (mailboxId: string) => { };
|
|
|
|
|
private mailboxDeleteHandler = (mailboxId: string) => { };
|
2024-07-06 14:11:48 +02:00
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// INIT
|
2024-07-06 14:11:48 +02:00
|
|
|
set onconnect(handler: () => void) {
|
|
|
|
|
this.connectionHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
set ondisconnect(handler: () => void) {
|
|
|
|
|
this.disconnectionHandler = handler;
|
|
|
|
|
}
|
|
|
|
|
set onmessage(handler: (data: Message) => void) {
|
|
|
|
|
this.messageHandler = handler;
|
|
|
|
|
}
|
2024-07-09 00:30:57 +02:00
|
|
|
set onmailboxcreate(handler: (mailboxId: string) => void) {
|
2024-07-08 21:13:56 +02:00
|
|
|
this.mailboxHandler = handler;
|
|
|
|
|
}
|
2024-07-08 23:34:16 +02:00
|
|
|
set onmailboxconnect(handler: (mailboxId: string) => void) {
|
|
|
|
|
this.mailboxConnectionHandler = handler;
|
|
|
|
|
}
|
2024-07-09 00:30:57 +02:00
|
|
|
set onmailboxdelete(handler: (mailboxId: string) => void) {
|
|
|
|
|
this.mailboxDeleteHandler = handler;
|
|
|
|
|
}
|
2024-07-06 14:11:48 +02:00
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// UTILITY METHODS
|
2024-07-08 21:13:56 +02:00
|
|
|
private send(messageObject: Message): boolean {
|
2024-07-06 14:11:48 +02:00
|
|
|
if (this.ws == undefined) return false;
|
2026-10-12 20:38:04 +02:00
|
|
|
if (this.ws.readyState != 1) return false;
|
2024-07-06 14:11:48 +02:00
|
|
|
|
|
|
|
|
const messageString = JSON.stringify(messageObject);
|
|
|
|
|
this.ws.send(messageString);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// PUBLIC METHODS
|
|
|
|
|
// connection
|
2024-07-06 14:11:48 +02:00
|
|
|
connect(address: string): void {
|
2024-07-09 00:30:57 +02:00
|
|
|
try {
|
|
|
|
|
this.disconnect();
|
2024-07-08 21:13:56 +02:00
|
|
|
|
2024-07-09 00:30:57 +02:00
|
|
|
this.ws = new WebSocket(address);
|
|
|
|
|
this.ws.addEventListener("open", this.connectionHandler);
|
|
|
|
|
this.ws.addEventListener("close", this.disconnectionHandler);
|
|
|
|
|
this.ws.addEventListener("message", (message) => {
|
|
|
|
|
const dataString = message.data.toString();
|
|
|
|
|
const data = JSON.parse(dataString);
|
2024-07-08 21:13:56 +02:00
|
|
|
|
2024-07-09 00:30:57 +02:00
|
|
|
if (data.assignedMailboxId) {
|
|
|
|
|
return this.mailboxHandler(data.assignedMailboxId);
|
|
|
|
|
} else if (data.connectedMailboxId) {
|
|
|
|
|
return this.mailboxConnectionHandler(data.connectedMailboxId);
|
|
|
|
|
} else if (data.deletedMailbox) {
|
|
|
|
|
return this.mailboxDeleteHandler(data.deletedMailbox);
|
|
|
|
|
} else {
|
2026-10-12 20:38:04 +02:00
|
|
|
this.send({ uuid: data.uuid, confirmingMessageReceived: true });
|
2024-07-09 00:30:57 +02:00
|
|
|
this.messageHandler(data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
2024-07-06 14:11:48 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-08 21:13:56 +02:00
|
|
|
disconnect(): void {
|
|
|
|
|
this.ws?.close();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// message
|
2024-07-06 14:11:48 +02:00
|
|
|
sendMessage(channel: string, body: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
messageChannel: channel,
|
|
|
|
|
messageBody: body,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// subscription
|
2024-07-06 14:11:48 +02:00
|
|
|
subscribe(channel: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
subscribeChannel: channel,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsubscribe(channel: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
unsubscribeChannel: channel,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
2024-07-08 21:13:56 +02:00
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
// mailbox
|
2024-07-08 21:13:56 +02:00
|
|
|
requestMailbox(): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
requestingMailboxSetup: true,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 23:34:16 +02:00
|
|
|
connectMailbox(mailboxId: string): boolean {
|
2024-07-08 21:13:56 +02:00
|
|
|
const messageObject = {
|
|
|
|
|
requestedMailbox: mailboxId,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
2024-07-08 23:34:16 +02:00
|
|
|
|
|
|
|
|
deleteMailbox(mailboxId: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
deletingMailbox: mailboxId,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
2024-07-06 14:11:48 +02:00
|
|
|
}
|