source/lib/VBDiscover.js
// These should be received whenever a device connects to the network

These should be received whenever a device connects to the network
These should be received whenever a device connects to the network
function ssdpNotify(discoveryCallback) {
  let notifySocket = dgram.createSocket({type: 'udp4', reuseAddr: true})
  notifySocket.on('message', (msg, rinfo) => {
    const ssdpResponse = msg.toString()
    const serialNumber = extract(reSerialNumber, ssdpResponse)
    const ipAddr = extract(reIpAddr, ssdpResponse)
    if (ipAddr && serialNumber) {
      deviceDiscovered(ipAddr, serialNumber, discoveryCallback)
    }
  })
  notifySocket.on('error', (e) => {
    console.log('notifySocket error: %O', e)
  })
  notifySocket.bind(1900, () => {
    try {
      notifySocket.setMulticastLoopback(false)
      notifySocket.addMembership('239.255.255.250')
    }
    catch (e) {
      console.log('notifySocket.bind exception: %O', e)
    }
  })
  setTimeout( () => {
    try {
      notifySocket.close( () => {ssdpNotify(discoveryCallback)} )
    }
    catch (e) {
      console.log('Exception when trying to close socket: %O', e)
    }
  }, 5 * 60 * 1000 )
}
function ssdpNotify ( discoveryCallback ) { let notifySocket = dgram . createSocket ( { type : 'udp4' , reuseAddr : true } ) notifySocket . on ( 'message' , ( msg , rinfo ) => { const ssdpResponse = msg . toString ( ) const serialNumber = extract ( reSerialNumber , ssdpResponse ) const ipAddr = extract ( reIpAddr , ssdpResponse ) if ( ipAddr && serialNumber ) { deviceDiscovered ( ipAddr , serialNumber , discoveryCallback ) } } ) notifySocket . on ( 'error' , ( e ) => { console . log ( 'notifySocket error: %O' , e ) } ) notifySocket . bind ( 1900 , ( ) => { try { notifySocket . setMulticastLoopback ( false ) notifySocket . addMembership ( '239.255.255.250' ) } catch ( e ) { console . log ( 'notifySocket.bind exception: %O' , e ) } } ) setTimeout ( ( ) => { try { notifySocket . close ( ( ) => { ssdpNotify ( discoveryCallback ) } ) } catch ( e ) { console . log ( 'Exception when trying to close socket: %O' , e ) } } , 5 * 60 * 1000 ) }
These should be received whenever a device connects to the network
These should be received whenever a device connects to the network
// Only add devices that have an ip address AND Roku serial number,
 // to avoid sending ECP requests to non-Roku devices.
 // If binding fails, an 'error' event is generated
 // However, in some cases, an exception may be thrown,
 // hence the 'try-catch' in the bind() function
 // SSDP NOTIFY responses are directed to port 1900
 // Prevent receipt of local SSDP M-SEARCH requests
 // Join the SSDP multicast group so we can receive SSDP NOTIFY responses
 // If the network connection drops, then no further NOTIFY responses
 // will be received on the bound port
 // Since there is no indication of a network connection failure,
 // after a predetermined timeout, close then re-establish the connection
