Skip to content

Commit

Permalink
Adds a generic error handler on the primary client to catch errors th…
Browse files Browse the repository at this point in the history
…at occur within another error, and changes order of disconnect cleanup routine
  • Loading branch information
i8beef committed Nov 2, 2021
1 parent 57207f4 commit 7f97160
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions castv2-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = function(RED) {
const net = require('net');

const Client = require('castv2-client').Client;
const Bonjour = require('bonjour');

const MediaReceiverBase = require('./lib/MediaReceiverBase');
const DefaultMediaReceiver = require('./lib/DefaultMediaReceiver');
Expand Down Expand Up @@ -160,13 +159,10 @@ module.exports = function(RED) {
*/
this.disconnect = function() {
if (node.connected || node.connecting) {
// Ignore errors
try { node.client.close(); } catch (exception) { }
}

// Reset client
node.client = null;
node.platformStatus = null;
// Set connection status
node.connected = false;
node.connecting = false;

Expand All @@ -177,6 +173,10 @@ module.exports = function(RED) {
}
}

// Reset client
node.client = null;
node.platformStatus = null;

node.setStatusOfRegisteredNodes({ fill: "red", shape: "ring", text: "disconnected" });
};

Expand Down Expand Up @@ -219,12 +219,17 @@ module.exports = function(RED) {
node.client.setVolumeAsync = util.promisify(node.client.setVolume);
node.client.stopAsync = util.promisify(node.client.stop);

// Register secondary error handler
node.client.on("error", function(error) {
console.log(error);
});

// Register error handler
node.client.once("error", function(error) {
node.disconnect();
node.reconnect();
});

// Register disconnect handlers
node.client.client.once("close", function() {
node.disconnect();
Expand Down Expand Up @@ -284,7 +289,7 @@ module.exports = function(RED) {
console.log(error);
node.disconnect();
node.reconnect();
});
});
} catch (exception) { console.log(exception); }
}
};
Expand Down Expand Up @@ -409,7 +414,6 @@ module.exports = function(RED) {
node.adapter = null;

if (node.receiver != null) {
// Ignore errors
try { node.receiver.close(); } catch (e) { }
node.receiver = null;
}
Expand Down Expand Up @@ -691,7 +695,6 @@ module.exports = function(RED) {
node.adapter = null;

if (node.receiver != null) {
// Ignore errors
try { node.receiver.close(); } catch (e) { }
node.receiver = null;
}
Expand Down

0 comments on commit 7f97160

Please sign in to comment.