Files
UDN/static/node_modules/udn-frontend/index.ts
T

130 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-07-06 14:11:48 +02:00
export interface Message {
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-08 21:13:56 +02:00
2024-07-06 14:11:48 +02:00
// subscribing to channel
subscribeChannel?: string;
unsubscribeChannel?: string;
2024-07-08 21:13:56 +02:00
subscribed?: boolean; // sent by server to confirm subscription
2024-07-06 14:11:48 +02:00
//sending message
messageChannel?: string;
messageBody?: string;
}
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
2024-07-06 14:11:48 +02:00
private connectionHandler = () => {};
private disconnectionHandler = () => {};
private messageHandler = (data: Message) => {};
2024-07-08 21:13:56 +02:00
private mailboxHandler = (mailboxId: string) => {};
2024-07-08 23:34:16 +02:00
private mailboxConnectionHandler = (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-08 21:13:56 +02:00
set onmailbox(handler: (mailboxId: string) => void) {
this.mailboxHandler = handler;
}
2024-07-08 23:34:16 +02:00
set onmailboxconnect(handler: (mailboxId: string) => void) {
this.mailboxConnectionHandler = 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;
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-08 21:13:56 +02:00
this.disconnect();
2024-07-06 14:11:48 +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
if (data.assignedMailboxId) {
return this.mailboxHandler(data.assignedMailboxId);
2024-07-08 23:34:16 +02:00
} else if (data.connectedMailboxId) {
return this.mailboxConnectionHandler(data.assignedMailboxId);
2024-07-08 21:13:56 +02:00
} else {
this.messageHandler(data);
}
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
}