mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-08 08:39:59 +00:00
Add some basic logging
This commit is contained in:
@@ -13,6 +13,7 @@ interface CastClientConnectOptions {
|
||||
port?: number;
|
||||
onReceiverMessage?: (message: ReceiverMessage) => void;
|
||||
onHeartbeat?: () => void;
|
||||
onClose?: () => void;
|
||||
}
|
||||
|
||||
export default class CastClient {
|
||||
@@ -55,6 +56,8 @@ export default class CastClient {
|
||||
sendReceiverMessage(message: DistributiveOmit<SenderMessage, "requestId">) {
|
||||
if (!this.receiverChannel) return;
|
||||
|
||||
console.error("[CastClient] Sending receiver message", message);
|
||||
|
||||
const requestId = this.receiverRequestId++;
|
||||
this.receiverChannel.send({ ...message, requestId });
|
||||
return requestId;
|
||||
@@ -66,12 +69,25 @@ export default class CastClient {
|
||||
*/
|
||||
connect(host: string, options?: CastClientConnectOptions) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
let isPromiseResolved = false;
|
||||
|
||||
// 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", () => {
|
||||
console.error(`[CastClient] Client at ${host} closed!`);
|
||||
if (this.heartbeatChannel && this.heartbeatIntervalId) {
|
||||
clearInterval(this.heartbeatIntervalId);
|
||||
}
|
||||
options?.onClose?.();
|
||||
});
|
||||
|
||||
this.client.connect(
|
||||
@@ -98,6 +114,7 @@ export default class CastClient {
|
||||
options?.onHeartbeat?.();
|
||||
}, HEARTBEAT_INTERVAL_MS);
|
||||
|
||||
isPromiseResolved = true;
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
|
||||
@@ -71,16 +71,35 @@ export default class Remote extends CastClient {
|
||||
|
||||
// Handle app creation/discovery
|
||||
if (application && !this.transportClient) {
|
||||
console.error(
|
||||
`[Remote]: Creating transport at ${this.host} for: `,
|
||||
{ application }
|
||||
);
|
||||
|
||||
this.transportClient = new RemoteTransport(
|
||||
application.transportId,
|
||||
message => this.onMediaMessage(message)
|
||||
);
|
||||
|
||||
this.transportClient.connect(this.host).then(() => {
|
||||
this.transportClient
|
||||
.connect(this.host, {
|
||||
onClose: () => {
|
||||
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?.();
|
||||
@@ -116,6 +135,15 @@ class RemoteTransport extends CastClient {
|
||||
}
|
||||
|
||||
sendMediaMessage(message: SenderMediaMessage) {
|
||||
console.error("[Remote] Sending receiver message", message);
|
||||
|
||||
try {
|
||||
this.mediaChannel.send(message);
|
||||
} catch (err) {
|
||||
console.error("[Remote] Failed to send media message!", {
|
||||
message,
|
||||
err
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,9 @@ messaging.on("message", (message: Message) => {
|
||||
});
|
||||
|
||||
if (shouldWatchStatus) {
|
||||
console.error(
|
||||
`Device found (id: ${device.id}, name: ${device.friendlyName}, host: ${device.host}), removing remote!`
|
||||
);
|
||||
remotes.set(
|
||||
device.id,
|
||||
new Remote(device.host, {
|
||||
@@ -90,6 +93,9 @@ messaging.on("message", (message: Message) => {
|
||||
|
||||
if (shouldWatchStatus) {
|
||||
if (remotes.has(deviceId)) {
|
||||
console.error(
|
||||
`Device lost (id: ${deviceId}), removing remote!`
|
||||
);
|
||||
remotes.get(deviceId)?.disconnect();
|
||||
remotes.delete(deviceId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user