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 = { var storageKeys = {
// connection // connection
socketAddress: [DATA_VERSION, "connection", "socket-address"], socketAddress: [DATA_VERSION, "connection", "socket-address"],
reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"],
outbox: [DATA_VERSION, "connection", "outbox"], outbox: [DATA_VERSION, "connection", "outbox"],
// settings // settings
username: [DATA_VERSION, "settings", "user-name"], username: [DATA_VERSION, "settings", "user-name"],
@@ -1504,8 +1505,11 @@
connect = (address) => { connect = (address) => {
this.udn.connect(address); this.udn.connect(address);
}; };
a;
disconnect = () => { disconnect = () => {
this.udn.disconnect(); this.udn.disconnect();
const reconnectAddressPath = storageKeys.reconnectAddress;
this.storageModel.remove(reconnectAddressPath);
}; };
handleMessage = (data) => { handleMessage = (data) => {
this.messageHandler(data); this.messageHandler(data);
@@ -1525,6 +1529,7 @@
this.sendSubscriptionRequest(); this.sendSubscriptionRequest();
}; };
sendSubscriptionRequest = () => { sendSubscriptionRequest = () => {
if (this.isConnected == false) return;
for (const channel of this.channelsToSubscribe) { for (const channel of this.channelsToSubscribe) {
this.udn.subscribe(channel); this.udn.subscribe(channel);
} }
@@ -1579,6 +1584,9 @@
storeAddress = (address) => { storeAddress = (address) => {
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, ""); this.storageModel.store(addressPath, "");
const reconnectAddressPath = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address);
this.storageModel.print();
}; };
removeAddress = (address) => { removeAddress = (address) => {
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
@@ -1608,6 +1616,11 @@
this.udn.ondisconnect = () => { this.udn.ondisconnect = () => {
this.handleConnectionChange(); 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 => { connect = (address: string): void => {
this.udn.connect(address); this.udn.connect(address);
}; };
a;
disconnect = (): void => { disconnect = (): void => {
this.udn.disconnect(); this.udn.disconnect();
// do not reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.remove(reconnectAddressPath);
}; };
handleMessage = (data: Message): void => { handleMessage = (data: Message): void => {
@@ -41,6 +45,7 @@ export default class ConnectionModel {
handleConnectionChange = (): void => { handleConnectionChange = (): void => {
console.log("connection status:", this.isConnected, this.address); console.log("connection status:", this.isConnected, this.address);
this.connectionChangeHandler(); this.connectionChangeHandler();
if (this.isConnected == false) return; if (this.isConnected == false) return;
if (!this.address) return; if (!this.address) return;
@@ -56,6 +61,8 @@ export default class ConnectionModel {
}; };
sendSubscriptionRequest = (): void => { sendSubscriptionRequest = (): void => {
if (this.isConnected == false) return;
for (const channel of this.channelsToSubscribe) { for (const channel of this.channelsToSubscribe) {
this.udn.subscribe(channel); this.udn.subscribe(channel);
} }
@@ -122,8 +129,14 @@ export default class ConnectionModel {
}; };
storeAddress = (address: string): void => { storeAddress = (address: string): void => {
// history
const addressPath = this.getAddressPath(address); const addressPath = this.getAddressPath(address);
this.storageModel.store(addressPath, ""); this.storageModel.store(addressPath, "");
// reconnect
const reconnectAddressPath: string[] = storageKeys.reconnectAddress;
this.storageModel.store(reconnectAddressPath, address);
this.storageModel.print();
}; };
removeAddress = (address: string): void => { removeAddress = (address: string): void => {
@@ -161,5 +174,13 @@ export default class ConnectionModel {
this.udn.ondisconnect = () => { this.udn.ondisconnect = () => {
this.handleConnectionChange(); 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 = { export const storageKeys = {
// connection // connection
socketAddress: [DATA_VERSION, "connection", "socket-address"], socketAddress: [DATA_VERSION, "connection", "socket-address"],
reconnectAddress: [DATA_VERSION, "connection", "reconnect-address"],
outbox: [DATA_VERSION, "connection", "outbox"], outbox: [DATA_VERSION, "connection", "outbox"],
// settings // settings