@altronix/mdns
v0.0.9
Published
Atx mDNS responder library
Downloads
9
Readme
mdns-npm
Use
import { MdnsResponder } from '@altronix/mdns';
async function main() {
const responder = new MdnsResponder()
async function stopResponder() {
console.log('\nStopping mDNS responder...')
try {
await responder.stop()
console.log('mDNS responder stopped successfully.')
} catch (error) {
console.error('Error stopping mDNS responder:', error)
} finally {
process.exit(0)
}
}
try {
// Start advertising the first IP address
const firstIpAddress = '192.168.20.20'
console.log(`Starting mDNS responder with IP: ${firstIpAddress}`)
await responder.start(firstIpAddress)
try {
const interfaces = await responder.getNetworkInterfaces()
console.log(interfaces)
} catch (error) {
console.error('Error getting network interfaces:', error)
}
// Schedule changing the IP address after 3 minutes
setTimeout(async () => {
const secondIpAddress = '192.168.30.30'
console.log(`Changing advertised IP to: ${secondIpAddress}`)
await responder.changeIpAddress(secondIpAddress)
console.log('IP address changed successfully')
// Schedule stop advertising after another 3 minutes
setTimeout(async () => {
await stopResponder()
}, 3 * 60 * 1000) // 3 minutes in milliseconds
}, 3 * 60 * 1000) // 3 minutes in milliseconds
console.log('Press Ctrl+C to stop the responder.')
// Handle graceful shutdown
process.on('SIGINT', stopResponder)
process.on('SIGTERM', stopResponder)
} catch (error) {
console.error('Error in mDNS responder:', error)
process.exit(1)
}
}
main()