From 8ef946383a3e71d3660be22dd7ac9fad2e39534c Mon Sep 17 00:00:00 2001 From: Khaos Tian Date: Mon, 28 Nov 2016 13:35:58 -0800 Subject: [PATCH] Add ability to stop Accessory. Addresses #351 --- lib/Accessory.js | 13 +++++++++++++ lib/HAPServer.js | 5 +++++ lib/util/eventedhttp.js | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/lib/Accessory.js b/lib/Accessory.js index 1ca317a58..c11d83e67 100644 --- a/lib/Accessory.js +++ b/lib/Accessory.js @@ -496,6 +496,19 @@ Accessory.prototype.publish = function(info, allowInsecureRequest) { this._server.listen(targetPort); } +/** + * Removes this Accessory from the local network + * Accessory object will no longer vaild after invoking this method + * Trying to invoke publish() on the object will result undefined behavior + */ +Accessory.prototype.destory = function() { + this._server.stop(); + this._advertiser.stopAdvertising(); + + this._server = undefined; + this._advertiser = undefined; +} + Accessory.prototype._updateConfiguration = function() { if (this._advertiser && this._advertiser.isAdvertising()) { // get our accessory information in HAP format and determine if our configuration (that is, our diff --git a/lib/HAPServer.js b/lib/HAPServer.js index 78d816623..3eaa40780 100644 --- a/lib/HAPServer.js +++ b/lib/HAPServer.js @@ -158,6 +158,11 @@ HAPServer.prototype.listen = function(port) { this._httpServer.listen(port); } +HAPServer.prototype.stop = function() { + this._httpServer.stop(); + clearInterval(this._keepAliveTimerID); +} + HAPServer.prototype._onKeepAliveTimerTick = function() { // send out a "keepalive" event which all connections automatically sign up for once pairVerify is // completed. The event contains no actual data, so iOS devices will simply ignore it. diff --git a/lib/util/eventedhttp.js b/lib/util/eventedhttp.js index 1b50fccb6..5a7d3481c 100644 --- a/lib/util/eventedhttp.js +++ b/lib/util/eventedhttp.js @@ -68,6 +68,11 @@ EventedHTTPServer.prototype.listen = function(targetPort) { this._tcpServer.on('connection', this._onConnection.bind(this)); } +EventedHTTPServer.prototype.stop = function() { + this._tcpServer.close(); + this._connections = []; +} + EventedHTTPServer.prototype.sendEvent = function(event, data, contentType, exclude) { for (var index in this._connections) { var connection = this._connections[index];