example.js 788 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var mdns = require('./')()
  2. mdns.on('warning', function (err) {
  3. console.log(err.stack)
  4. })
  5. mdns.on('response', function (response) {
  6. console.log('got a response packet:', response)
  7. })
  8. mdns.on('query', function (query) {
  9. console.log('got a query packet:', query)
  10. // iterate over all questions to check if we should respond
  11. query.questions.forEach(function (q) {
  12. if (q.type === 'A' && q.name === 'example.local') {
  13. // send an A-record response for example.local
  14. mdns.respond({
  15. answers: [{
  16. name: 'example.local',
  17. type: 'A',
  18. ttl: 300,
  19. data: '192.168.1.5'
  20. }]
  21. })
  22. }
  23. })
  24. })
  25. // lets query for an A-record for example.local
  26. mdns.query({
  27. questions: [{
  28. name: 'example.local',
  29. type: 'A'
  30. }]
  31. })