automatically reconnect to previous address

This commit is contained in:
marlon-erler
2024-07-21 03:41:10 +02:00
parent ee50d88066
commit a0932c2198
3 changed files with 36 additions and 1 deletions
+13
View File
@@ -347,6 +347,7 @@
var storageKeys = {
// connection
socketAddress: [DATA_VERSION, "connection", "socket-address"],
reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"],
outbox: [DATA_VERSION, "connection", "outbox"],
// settings
username: [DATA_VERSION, "settings", "user-name"],
@@ -1504,8 +1505,11 @@
connect = (address) => {
this.udn.connect(address);
};
a;
disconnect = () => {
this.udn.disconnect();
const reconnectAddressPath = storageKeys.reconnectAddress;
this.storageModel.remove(reconnectAddressPath);
};
handleMessage = (data) => {
this.messageHandler(data);
@@ -1525,6 +1529,7 @@
this.sendSubscriptionRequest();
};
sendSubscriptionRequest = () => {
if (this.isConnected == false) return;
for (const channel of this.channelsToSubscribe) {
this.udn.subscribe(channel);
}
@@ -1579,6 +1584,9 @@
storeAddress = (address) => {
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
const reconnectAddressPath = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address);
this.storageModel.print();
};
removeAddress = (address) => {
const addressPath = this.getAddressPath(address);
@@ -1608,6 +1616,11 @@
this.udn.ondisconnect = () => {
this.handleConnectionChange();
};
const reconnectAddressPath = storageKeys.reconnectAddress;
const reconnectAddress = storageModel2.restore(reconnectAddressPath);
if (reconnectAddress != null) {
this.connect(reconnectAddress);
}
}
};
+22 -1
View File
@@ -29,9 +29,13 @@ export default class ConnectionModel {
connect = (address: string): void => {
this.udn.connect(address);
};
a;
disconnect = (): void => {
this.udn.disconnect();
// do not reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.remove(reconnectAddressPath);
};
handleMessage = (data: Message): void => {
@@ -41,6 +45,7 @@ export default class ConnectionModel {
handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address);
this.connectionChangeHandler();
if (this.isConnected == false) return;
if (!this.address) return;
@@ -56,6 +61,8 @@ export default class ConnectionModel {
};
sendSubscriptionRequest = (): void => {
if (this.isConnected == false) return;
for (const channel of this.channelsToSubscribe) {
this.udn.subscribe(channel);
}
@@ -122,8 +129,14 @@ export default class ConnectionModel {
};
storeAddress = (address: string): void => {
// history
const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, "");
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address);
this.storageModel.print();
};
removeAddress = (address: string): void => {
@@ -161,5 +174,13 @@ export default class ConnectionModel {
this.udn.ondisconnect = () => {
this.handleConnectionChange();
};
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
const reconnectAddress: string | null =
storageModel.restore(reconnectAddressPath);
if (reconnectAddress != null) {
this.connect(reconnectAddress);
}
}
}
+1
View File
@@ -131,6 +131,7 @@ export type StorageEntry = { [key: string]: StorageEntry };
export const storageKeys = {
// connection
socketAddress: [DATA_VERSION, "connection", "socket-address"],
reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"],
outbox: [DATA_VERSION, "connection", "outbox"],
// settings