add mailboxes
This commit is contained in:
+19
-2
@@ -35,10 +35,18 @@ UDN.unsubscribe("my-channel");
|
||||
|
||||
```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 the server to confirm subscription
|
||||
subscribed?: boolean; // sent by server to confirm subscription
|
||||
|
||||
//sending message
|
||||
messageChannel?: string;
|
||||
@@ -46,7 +54,7 @@ export interface Message {
|
||||
}
|
||||
|
||||
export default class UDNFrontend {
|
||||
private ws: WebSocket | undefined;
|
||||
ws: WebSocket | undefined;
|
||||
|
||||
// handlers
|
||||
private connectionHandler = () => {};
|
||||
@@ -60,6 +68,7 @@ export default class UDNFrontend {
|
||||
|
||||
// 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
|
||||
@@ -75,3 +84,11 @@ export default class UDNFrontend {
|
||||
## 1.0.2
|
||||
|
||||
- minor fixes
|
||||
|
||||
## 1.0.3
|
||||
|
||||
- add `disconnect()` method
|
||||
|
||||
## 1.0.4
|
||||
|
||||
- make ws public
|
||||
+41
-4
@@ -1,8 +1,16 @@
|
||||
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 the server to confirm subscription
|
||||
subscribed?: boolean; // sent by server to confirm subscription
|
||||
|
||||
//sending message
|
||||
messageChannel?: string;
|
||||
@@ -10,12 +18,13 @@ export interface Message {
|
||||
}
|
||||
|
||||
export default class UDNFrontend {
|
||||
private ws: WebSocket | undefined;
|
||||
ws: WebSocket | undefined;
|
||||
|
||||
// handlers
|
||||
private connectionHandler = () => {};
|
||||
private disconnectionHandler = () => {};
|
||||
private messageHandler = (data: Message) => {};
|
||||
private mailboxHandler = (mailboxId: string) => {};
|
||||
|
||||
// init
|
||||
set onconnect(handler: () => void) {
|
||||
@@ -27,9 +36,12 @@ export default class UDNFrontend {
|
||||
set onmessage(handler: (data: Message) => void) {
|
||||
this.messageHandler = handler;
|
||||
}
|
||||
set onmailbox(handler: (mailboxId: string) => void) {
|
||||
this.mailboxHandler = handler;
|
||||
}
|
||||
|
||||
// utility methods
|
||||
private send(messageObject: Object): boolean {
|
||||
private send(messageObject: Message): boolean {
|
||||
if (this.ws == undefined) return false;
|
||||
|
||||
const messageString = JSON.stringify(messageObject);
|
||||
@@ -39,16 +51,27 @@ export default class UDNFrontend {
|
||||
|
||||
// public methods
|
||||
connect(address: string): void {
|
||||
this.disconnect();
|
||||
|
||||
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);
|
||||
this.messageHandler(data);
|
||||
|
||||
if (data.assignedMailboxId) {
|
||||
return this.mailboxHandler(data.assignedMailboxId);
|
||||
} else {
|
||||
this.messageHandler(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
this.ws?.close();
|
||||
}
|
||||
|
||||
sendMessage(channel: string, body: string): boolean {
|
||||
const messageObject = {
|
||||
messageChannel: channel,
|
||||
@@ -70,4 +93,18 @@ export default class UDNFrontend {
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
requestMailbox(): boolean {
|
||||
const messageObject = {
|
||||
requestingMailboxSetup: true,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
|
||||
accessMailbox(mailboxId: string): boolean {
|
||||
const messageObject = {
|
||||
requestedMailbox: mailboxId,
|
||||
};
|
||||
return this.send(messageObject);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "udn-frontend",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.4-beta2",
|
||||
"description": "TypeScript library for UDN apps",
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user