diff --git a/mdns-render.js b/mdns-render.js index 95c978e..a642d74 100644 --- a/mdns-render.js +++ b/mdns-render.js @@ -4,7 +4,65 @@ var electron = require('electron'); var ipc = electron.ipcRenderer; ipc.on('deviceFound', function (e, packet) { - console.log(JSON.stringify(packet)); + var name = packet.answer[0].data.replace('.' + packet.answer[0].name, ''); + var ips = []; + + packet.additional.forEach(function(record) { + if (record.name.indexOf(name) !== 0) { + console.warn('unexpected record name ' + record.name, record); + return; + } + + if (record.typeName === 'A') { + ips.push({ family: 'ipv4', address: record.address }); + } + else if (record.typeName === 'AAAA') { + ips.push({ family: 'ipv6', address: record.address }); + } + else { + console.log('mDNS record not handled', record); + } + }, this); + + ips.sort(function (addrA, addrB) { + if (addrA.family < addrB.family) { + return -1; + } else if (addrA.family > addrB.family) { + return 1; + } + + else if (addrA.address < addrB.address) { + return -1; + } else if (addrA.address > addrB.address) { + return 1; + } + return 0; + }); + + var container = document.querySelector('.found-devices'); + Array.prototype.slice.call(container.children).forEach(function (elem) { + if (elem.classList.contains(name)) { + container.removeChild(elem); + } + }); + + var elem = document.createElement('div'); + elem.classList.add('device'); + elem.classList.add(name); + + var header = document.createElement('h4'); + header.textContent = name; + elem.appendChild(header); + + var list = document.createElement('ul'); + ips.forEach(function (addr) { + var subElem = document.createElement('li'); + subElem.textContent = addr.address; + list.appendChild(subElem); + }); + elem.appendChild(list); + + container.appendChild(elem); }); document.body.addEventListener('click', function (ev) { diff --git a/package.json b/package.json index ed1ab29..42c616b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "electron-demo", "productName": "ElectronDemo", - "version": "0.0.5", + "version": "0.0.6", "description": "Demo Electron app to use all the 'sexy' features", "author": { "name": "Daplie Labs",