improve mailboxes

This commit is contained in:
marlon-erler
2024-07-08 23:34:16 +02:00
parent 93658124cc
commit a8186e79be
13 changed files with 260 additions and 187 deletions
+17 -63
View File
@@ -13,82 +13,36 @@ import UDNFrontend from "udn-frontend";
// setup
const UDN = new UDNFrontend();
// connect
UDN.onconnect = () => {
console.log("connected!");
};
UDN.ondisconnect = () => {
console.log("disconnected!");
};
UDN.connect("ws://192.168.0.69:1234");
UDN.disconnect();
// messages
UDN.onmessage = (data: Message) => {
console.log(data);
};
// do stuff
UDN.connect("ws://192.168.0.69:1234");
UDN.subscribe("my-channel");
UDN.sendMessage("my-channel", "Hello, world!");
// subscribe
UDN.subscribe("my-channel");
UDN.unsubscribe("my-channel");
```
# Type Reference
```TypeScript
export interface Message {
// connecting to server
requestingServerConnection?: boolean;
// mailbox
requestingMailboxSetup?: boolean;
assignedMailboxId?: string; // sent by server
requestedMailbox?: string;
// subscribing to channel
subscribeChannel?: string;
unsubscribeChannel?: string;
subscribed?: boolean; // sent by server to confirm subscription
//sending message
messageChannel?: string;
messageBody?: string;
// mailbox
UDN.onmailbox = (mailboxId: string) => {
UDN.connectMailbox(mailboxId);
}
UDN.onmailboxconnect = (mailboxId: string) => {
UDN.deleteMailbox(mailboxId);
}
export default class UDNFrontend {
ws: WebSocket | undefined;
// handlers
private connectionHandler = () => {};
private disconnectionHandler = () => {};
private messageHandler = (data: Message) => {};
// init
set onconnect(handler: () => void);
set ondisconnect(handler: () => void);
set onmessage(handler: (data: Message) => void);
// methods
connect(address: string): void;
disconnect(address: string): void;
sendMessage(channel: string, body: string): boolean; // true if message is sent
subscribe(channel: string): boolean; // true if request is sent, NOT inherently when subscribed
unsubscribe(channel: string): boolean; // true if request is sent, NOT inherently when unsubscribed
}
UDN.requestMailbox();
```
# Changelog
## 1.0.1
- minor fixes
## 1.0.2
- minor fixes
## 1.0.3
- add `disconnect()` method
## 1.0.4
- make ws public