2024-07-08 13:07:15 +02:00
|
|
|
# udn-frontend
|
|
|
|
|
|
|
|
|
|
TypeScript library for [UDN](https://github.com/marlon-erler/universal-decentralized-network) apps
|
|
|
|
|
|
|
|
|
|
# Install
|
|
|
|
|
|
|
|
|
|
`npm install udn-frontend`
|
|
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
|
|
|
|
|
```TypeScript
|
|
|
|
|
import UDNFrontend from "udn-frontend";
|
|
|
|
|
|
|
|
|
|
// setup
|
|
|
|
|
const UDN = new UDNFrontend();
|
|
|
|
|
UDN.onconnect = () => {
|
|
|
|
|
console.log("connected!");
|
|
|
|
|
};
|
|
|
|
|
UDN.ondisconnect = () => {
|
|
|
|
|
console.log("disconnected!");
|
|
|
|
|
};
|
|
|
|
|
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!");
|
|
|
|
|
UDN.unsubscribe("my-channel");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# Type Reference
|
|
|
|
|
|
|
|
|
|
```TypeScript
|
|
|
|
|
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-08 13:07:15 +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-08 13:07:15 +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-08 13:07:15 +02:00
|
|
|
|
|
|
|
|
// 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;
|
2024-07-08 21:13:56 +02:00
|
|
|
disconnect(address: string): void;
|
2024-07-08 13:07:15 +02:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# Changelog
|
|
|
|
|
|
|
|
|
|
## 1.0.1
|
|
|
|
|
|
|
|
|
|
- minor fixes
|
|
|
|
|
|
|
|
|
|
## 1.0.2
|
|
|
|
|
|
|
|
|
|
- minor fixes
|
2024-07-08 21:13:56 +02:00
|
|
|
|
|
|
|
|
## 1.0.3
|
|
|
|
|
|
|
|
|
|
- add `disconnect()` method
|
|
|
|
|
|
|
|
|
|
## 1.0.4
|
|
|
|
|
|
|
|
|
|
- make ws public
|