Add some basic logging

This commit is contained in:
hensm
2023-10-20 20:42:46 +01:00
parent c9958231ea
commit 4e4c868c15
3 changed files with 58 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ interface CastClientConnectOptions {
port?: number; port?: number;
onReceiverMessage?: (message: ReceiverMessage) => void; onReceiverMessage?: (message: ReceiverMessage) => void;
onHeartbeat?: () => void; onHeartbeat?: () => void;
onClose?: () => void;
} }
export default class CastClient { export default class CastClient {
@@ -55,6 +56,8 @@ export default class CastClient {
sendReceiverMessage(message: DistributiveOmit<SenderMessage, "requestId">) { sendReceiverMessage(message: DistributiveOmit<SenderMessage, "requestId">) {
if (!this.receiverChannel) return; if (!this.receiverChannel) return;
console.error("[CastClient] Sending receiver message", message);
const requestId = this.receiverRequestId++; const requestId = this.receiverRequestId++;
this.receiverChannel.send({ ...message, requestId }); this.receiverChannel.send({ ...message, requestId });
return requestId; return requestId;
@@ -66,12 +69,25 @@ export default class CastClient {
*/ */
connect(host: string, options?: CastClientConnectOptions) { connect(host: string, options?: CastClientConnectOptions) {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
let isPromiseResolved = false;
// Handle errors // Handle errors
this.client.on("error", reject); this.client.on("error", err => {
if (!isPromiseResolved) {
isPromiseResolved = true;
reject();
}
console.error(`[CastClient] Client at ${host} gave error:`, {
err
});
});
this.client.on("close", () => { this.client.on("close", () => {
console.error(`[CastClient] Client at ${host} closed!`);
if (this.heartbeatChannel && this.heartbeatIntervalId) { if (this.heartbeatChannel && this.heartbeatIntervalId) {
clearInterval(this.heartbeatIntervalId); clearInterval(this.heartbeatIntervalId);
} }
options?.onClose?.();
}); });
this.client.connect( this.client.connect(
@@ -98,6 +114,7 @@ export default class CastClient {
options?.onHeartbeat?.(); options?.onHeartbeat?.();
}, HEARTBEAT_INTERVAL_MS); }, HEARTBEAT_INTERVAL_MS);
isPromiseResolved = true;
resolve(); resolve();
} }
); );

View File

@@ -71,17 +71,36 @@ export default class Remote extends CastClient {
// Handle app creation/discovery // Handle app creation/discovery
if (application && !this.transportClient) { if (application && !this.transportClient) {
console.error(
`[Remote]: Creating transport at ${this.host} for: `,
{ application }
);
this.transportClient = new RemoteTransport( this.transportClient = new RemoteTransport(
application.transportId, application.transportId,
message => this.onMediaMessage(message) message => this.onMediaMessage(message)
); );
this.transportClient.connect(this.host).then(() => { this.transportClient
this.transportClient?.sendMediaMessage({ .connect(this.host, {
type: "GET_STATUS", onClose: () => {
requestId: 0 console.error(
`[Remote] Transport at ${this.host} client closed!`
);
this.transportClient = undefined;
}
})
.then(() => {
this.transportClient?.sendMediaMessage({
type: "GET_STATUS",
requestId: 0
});
})
.catch(() => {
console.error(
`[Remote] Failed to create transport at ${this.host}`
);
}); });
});
this.options?.onApplicationFound?.(); this.options?.onApplicationFound?.();
} }
@@ -116,6 +135,15 @@ class RemoteTransport extends CastClient {
} }
sendMediaMessage(message: SenderMediaMessage) { sendMediaMessage(message: SenderMediaMessage) {
this.mediaChannel.send(message); console.error("[Remote] Sending receiver message", message);
try {
this.mediaChannel.send(message);
} catch (err) {
console.error("[Remote] Failed to send media message!", {
message,
err
});
}
} }
} }

View File

@@ -51,6 +51,9 @@ messaging.on("message", (message: Message) => {
}); });
if (shouldWatchStatus) { if (shouldWatchStatus) {
console.error(
`Device found (id: ${device.id}, name: ${device.friendlyName}, host: ${device.host}), removing remote!`
);
remotes.set( remotes.set(
device.id, device.id,
new Remote(device.host, { new Remote(device.host, {
@@ -90,6 +93,9 @@ messaging.on("message", (message: Message) => {
if (shouldWatchStatus) { if (shouldWatchStatus) {
if (remotes.has(deviceId)) { if (remotes.has(deviceId)) {
console.error(
`Device lost (id: ${deviceId}), removing remote!`
);
remotes.get(deviceId)?.disconnect(); remotes.get(deviceId)?.disconnect();
remotes.delete(deviceId); remotes.delete(deviceId);
} }