Skip to content

Commit

Permalink
* update * (Apollon77) Add experimental flag options.subscriptionsUse…
Browse files Browse the repository at this point in the history
…SameConnection for HTTP Client to use the same connection for subscriptions and for all other calls to only have one connection from controller to the device.
  • Loading branch information
Apollon77 committed May 10, 2022
1 parent a1105dc commit 3a3958c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ For Issues please consider to directly provide debug loggins (see above).

## Changelog

### __WORK IN PROGRESS__
* (Apollon77) Add experimental flag options.subscriptionsUseSameConnection for HTTP Client to use the same connection for subscriptions and for all other calls to only have one connection from controller to the device.

### 0.7.4 (2022-05-06)
* (Apollon77) Add Host header to all HTTP calls because some devices seem to require it
* (Apollon77) Check that client was initialized before accessing the connected state
Expand Down
45 changes: 35 additions & 10 deletions src/transport/ip/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,17 @@ interface EventCharacteristicsObject {
interface HttpClientOptions {
/**
* Set to true to use persistent connections for normal device interactions
* Without persistent connections a new pairing verification is rerquired
* Without persistent connections a new pairing verification is required
* before each call which delays the execution.
*/
usePersistentConnections: boolean;

/**
* Set this to true to use the same persistent connection for subscriptions
* as also for normal device interactions. This basically means that only
* one connection to the device ist used
*/
subscriptionsUseSameConnection: boolean;
}

export default class HttpClient extends EventEmitter {
Expand All @@ -133,6 +140,8 @@ export default class HttpClient extends EventEmitter {

private usePersistentConnections = true;

private subscriptionsUseSameConnection = false;

private subscriptionConnection?: HttpConnection;

private subscribedCharacteristics: string[] = [];
Expand Down Expand Up @@ -162,6 +171,7 @@ export default class HttpClient extends EventEmitter {
this.pairingProtocol = new PairingProtocol(pairingData);
this.pairingQueue = new OpQueue();
this.usePersistentConnections = options?.usePersistentConnections || false;
this.subscriptionsUseSameConnection = options?.subscriptionsUseSameConnection || false;
}

/**
Expand Down Expand Up @@ -195,6 +205,7 @@ export default class HttpClient extends EventEmitter {
* Checks if a maybe persistent connection should be closed
*
* @param {HttpConnection} connection Connection which was returned by getDefaultVerifiedConnection()
* @param {boolean} forceClose - Force close the connection
* @private
*/
private closeMaybePersistentConnection(connection: HttpConnection, forceClose = false): void {
Expand Down Expand Up @@ -597,13 +608,18 @@ export default class HttpClient extends EventEmitter {
* @returns {Promise} Promise
*/
async subscribeCharacteristics(characteristics: string[]): Promise<void> {
const connection = this.subscriptionConnection || new HttpConnection(this.address, this.port);
let connection: HttpConnection;
if (this.subscriptionsUseSameConnection) {
connection = await this.getDefaultVerifiedConnection();
} else {
connection = this.subscriptionConnection || new HttpConnection(this.address, this.port);
}

const data = {
characteristics: <EventCharacteristicsObject[]>[],
};

if (!this.subscriptionConnection) {
if (!this.subscriptionConnection && !this.subscriptionsUseSameConnection) {
const keys = await this._pairVerify(connection);
connection.setSessionKeys(keys);
}
Expand Down Expand Up @@ -635,7 +651,8 @@ export default class HttpClient extends EventEmitter {
this.emit('event', JSON.parse(ev));
});

connection.on('disconnect', () => {
connection.once('disconnect', () => {
connection.removeAllListeners('event');
delete this.subscriptionConnection;
if (this.subscribedCharacteristics.length) {
/**
Expand All @@ -662,7 +679,10 @@ export default class HttpClient extends EventEmitter {

if (response.statusCode !== 204 && response.statusCode !== 207) {
if (!this.subscribedCharacteristics.length) {
connection.close();
if (!this.subscriptionsUseSameConnection) {
connection.close();
}
connection.removeAllListeners('event');
delete this.subscriptionConnection;
}
throw new HomekitControllerError(
Expand Down Expand Up @@ -733,7 +753,10 @@ export default class HttpClient extends EventEmitter {
});

if (!this.subscribedCharacteristics.length) {
this.subscriptionConnection.close();
if (!this.subscriptionsUseSameConnection) {
this.subscriptionConnection.close();
}
this.subscriptionConnection?.removeAllListeners('event');
delete this.subscriptionConnection;
}
}
Expand Down Expand Up @@ -812,10 +835,12 @@ export default class HttpClient extends EventEmitter {
// ignore
}
delete this._pairingConnection;
try {
this.subscriptionConnection?.close();
} catch {
// ignore
if (!this.subscriptionsUseSameConnection) {
try {
this.subscriptionConnection?.close();
} catch {
// ignore
}
}
delete this.subscriptionConnection;
this.subscribedCharacteristics = [];
Expand Down

0 comments on commit 3a3958c

Please sign in to comment.