1
0
Fork 0
mirror of https://gitlab.com/futo-org/fcast.git synced 2025-08-13 19:02:50 +00:00
fcast/receivers/electron/src/DiscoveryService.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-06-20 08:45:01 +02:00
import mdns = require('mdns-js');
const cp = require('child_process');
const os = require('os');
export class DiscoveryService {
2023-12-30 10:55:30 +01:00
private serviceTcp: any;
private serviceWs: any;
2023-06-20 08:45:01 +02:00
private static getComputerName() {
switch (process.platform) {
case "win32":
return process.env.COMPUTERNAME;
case "darwin":
return cp.execSync("scutil --get ComputerName").toString().trim();
case "linux":
return os.hostname();
2023-06-20 08:45:01 +02:00
default:
return os.hostname();
}
}
start() {
2023-12-30 11:28:36 +01:00
if (this.serviceTcp || this.serviceWs) {
2023-06-20 08:45:01 +02:00
return;
}
const name = `FCast-${DiscoveryService.getComputerName()}`;
console.log("Discovery service started.", name);
2023-12-30 10:55:30 +01:00
this.serviceTcp = mdns.createAdvertisement(mdns.tcp('_fcast'), 46899, { name: name });
this.serviceTcp.start();
this.serviceWs = mdns.createAdvertisement(mdns.tcp('_fcast-ws'), 46898, { name: name });
this.serviceWs.start();
2023-06-20 08:45:01 +02:00
}
stop() {
2023-12-30 10:55:30 +01:00
if (this.serviceTcp) {
this.serviceTcp.stop();
this.serviceTcp = null;
}
if (this.serviceWs) {
this.serviceWs.stop();
this.serviceWs = null;
}
2023-06-20 08:45:01 +02:00
}
}