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;
|
|
|
|
|
assignedMailboxId?: string; // sent by server
|
|
|
|
|
requestedMailbox?: string;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// handlers
|
|
|
|
|
private connectionHandler = () => {};
|
|
|
|
|
private disconnectionHandler = () => {};
|
|
|
|
|
private messageHandler = (data: Message) => {};
|
2024-07-08 21:13:56 +02:00
|
|
|
private mailboxHandler = (mailboxId: string) => {};
|
2024-07-06 14:11:48 +02:00
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
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-06 14:11:48 +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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public methods
|
|
|
|
|
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);
|
|
|
|
|
} 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-06 14:11:48 +02:00
|
|
|
sendMessage(channel: string, body: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
messageChannel: channel,
|
|
|
|
|
messageBody: body,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
requestMailbox(): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
requestingMailboxSetup: true,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accessMailbox(mailboxId: string): boolean {
|
|
|
|
|
const messageObject = {
|
|
|
|
|
requestedMailbox: mailboxId,
|
|
|
|
|
};
|
|
|
|
|
return this.send(messageObject);
|
|
|
|
|
}
|
2024-07-06 14:11:48 +02:00
|
|
|
}
|