Improve session stopping

This commit is contained in:
hensm
2022-03-15 10:32:17 +00:00
parent 3c175a740d
commit 5120a3a7ab
6 changed files with 14 additions and 28 deletions

View File

@@ -26,7 +26,7 @@ export default class CastClient {
// Platform messaging // Platform messaging
private receiverChannel?: Channel; private receiverChannel?: Channel;
private receiverRequestId = 0; private receiverRequestId = Math.floor(Math.random() * 1e6);
constructor( constructor(
protected sourceId = "sender-0", protected sourceId = "sender-0",

View File

@@ -6,7 +6,7 @@ import { sendMessage } from "../../lib/nativeMessaging";
import { Message } from "../../messaging"; import { Message } from "../../messaging";
import Session from "./Session"; import Session from "./Session";
import { NS_CONNECTION, NS_RECEIVER } from "./client"; import CastClient, { NS_CONNECTION, NS_RECEIVER } from "./client";
const sessions = new Map<string, Session>(); const sessions = new Map<string, Session>();
@@ -112,29 +112,12 @@ export function handleCastMessage(message: Message) {
break; break;
} }
case "bridge:stopCastApp": { case "bridge:stopCastSession": {
const { host, port } = message.data.receiverDevice; const { receiverDevice } = message.data;
const client = new castv2.Client();
client.connect({ host, port }, () => { const client = new CastClient();
const sourceId = "sender-0"; client.connect(receiverDevice.host). then(() => {
const destinationId = "receiver-0"; (client.sendReceiverMessage as any)({ type: "STOP" });
const clientConnection = client.createChannel(
sourceId,
destinationId,
NS_CONNECTION,
"JSON"
);
const clientReceiver = client.createChannel(
sourceId,
destinationId,
NS_RECEIVER,
"JSON"
);
clientConnection.send({ type: "CONNECT" });
clientReceiver.send({ type: "STOP", requestId: 1 });
}); });
break; break;

View File

@@ -63,7 +63,9 @@ type MessageDefinitions = {
messageData: object | string; messageData: object | string;
messageId: string; messageId: string;
}; };
"bridge:stopCastApp": { receiverDevice: ReceiverDevice }; "bridge:stopCastSession": {
receiverDevice: ReceiverDevice;
};
/** /**
* getInfo uses the old :/ form for compat with old bridge * getInfo uses the old :/ form for compat with old bridge

View File

@@ -73,7 +73,7 @@ export default new (class extends TypedEventTarget<EventMap> {
const receiverDevice = this.receiverDevices.get(receiverDeviceId); const receiverDevice = this.receiverDevices.get(receiverDeviceId);
if (receiverDevice) { if (receiverDevice) {
this.bridgePort.postMessage({ this.bridgePort.postMessage({
subject: "bridge:stopCastApp", subject: "bridge:stopCastSession",
data: { receiverDevice } data: { receiverDevice }
}); });
} }

View File

@@ -200,7 +200,6 @@ async function getSelection(
"stop", "stop",
storeListener("stop", async ev => { storeListener("stop", async ev => {
logger.info("Stopping receiver app...", ev.detail); logger.info("Stopping receiver app...", ev.detail);
receiverDevices.stopReceiverApp(ev.detail.receiver.id); receiverDevices.stopReceiverApp(ev.detail.receiver.id);
resolve({ resolve({

View File

@@ -102,7 +102,9 @@ type AppMessageDefinitions = {
messageData: object | string; messageData: object | string;
messageId: string; messageId: string;
}; };
"bridge:stopCastApp": { receiverDevice: ReceiverDevice }; "bridge:stopCastSession": {
receiverDevice: ReceiverDevice;
};
/** /**
* getInfo uses the old :/ form for compat with old bridge * getInfo uses the old :/ form for compat with old bridge