source/lib/VBDiscover.js
// Invoke the callback to pass the device details back to the caller

Invoke the callback to pass the device details back to the caller
Invoke the callback to pass the device details back to the caller
function deviceDiscovered(ipAddr, serialNumber, discoveryCallback) {
  let remoteAddress = ''
  const bufferList = []
  const req = http.request({host: ipAddr, port: 8060, family: 4}, (res) => {
    res.on('data', (chunk) => {
      bufferList.push(chunk)
    })
    res.on('end', () => {
      const response = Buffer.concat(bufferList).toString()
      const ip = remoteAddress || ipAddr
      const details = parseDeviceDetails(ip, serialNumber, response)
      if (details.serialNumber) {
        discoveryCallback(details)
      }
    })
  })
  req.on('socket', (socket) => {
    socket.setTimeout(10000)
    socket.on('timeout', () => {
      console.log('deviceDiscovered socket timeout')
      req.abort()
    })
    socket.on('connect', () => {
      remoteAddress = socket.remoteAddress
    })
  })
  req.on('error', (error) => {
    const ip = remoteAddress || ipAddr
    const details = parseDeviceDetails(ip, serialNumber, '')
    if (details.serialNumber) {
      discoveryCallback(details)
    }
  })
  req.write('')
  req.end()
}
function deviceDiscovered ( ipAddr , serialNumber , discoveryCallback ) { let remoteAddress = '' const bufferList = [ ] const req = http . request ( { host : ipAddr , port : 8060 , family : 4 } , ( res ) => { res . on ( 'data' , ( chunk ) => { bufferList . push ( chunk ) } ) res . on ( 'end' , ( ) => { const response = Buffer . concat ( bufferList ) . toString ( ) const ip = remoteAddress || ipAddr const details = parseDeviceDetails ( ip , serialNumber , response ) if ( details . serialNumber ) { discoveryCallback ( details ) } } ) } ) req . on ( 'socket' , ( socket ) => { socket . setTimeout ( 10000 ) socket . on ( 'timeout' , ( ) => { console . log ( 'deviceDiscovered socket timeout' ) req . abort ( ) } ) socket . on ( 'connect' , ( ) => { remoteAddress = socket . remoteAddress } ) } ) req . on ( 'error' , ( error ) => { const ip = remoteAddress || ipAddr const details = parseDeviceDetails ( ip , serialNumber , '' ) if ( details . serialNumber ) { discoveryCallback ( details ) } } ) req . write ( '' ) req . end ( ) }
Invoke the callback to pass the device details back to the caller
Invoke the callback to pass the device details back to the caller
// Use the remoteAddress obtained from the socket event in case
 // an ECP request has been issued specifying a host name
 // rather than an IP address
 // A 'socket' event is emitted after a socket is assigned to the request
 // Handle this event to set a timeout on the socket connection
 // This is instead of setting the timeout when http.request() is called,
 // which would only be emitted after the socket is assigned and is connected,
 // and would not detect a timeout while trying to establish the connection
 // Additionally, we'll need to listen for the socket connect event so we
 // can obtain the remote IP address in case an ECP request was issued to
 // a host name rather than an IP address
 // A timeout does not abort the connection; it has to be done manually
 // This will cause a createHangUpError error to be emitted on the request
 // Listen for the socket connect event so we can determine the
 // IP address of the Roku, which will be necessary if an ECP request
 // was issue to a host name rather than an IP address; this will
 // be the earliest time we can determine what the IP address is
 // Even if there is an error on the ECP request, invoke the
 // discoveryCallback with the known ip address and serial number
 // Use the remoteAddress obtained from the socket connect event in case
 // an ECP request has been issued specifying a host name
 // rather than an IP address
 //console.log('deviceDiscovered error: %O', error)
 // The ECP request has an empty body
 // Send the ECP request
