Heroku - Headless Chrome - Connection Refused

浪尽此生 提交于 2019-12-10 19:12:03

问题


I am currently working with the: Heroku Build Pack for headless chrome. https://github.com/heroku/heroku-buildpack-google-chrome/

I'm encountering this infuriating error where my node script (show below) cannot connect to the chrome instance. I get a pretty definitive error being:

{ Error: connect ECONNREFUSED 127.0.0.1:30555
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
  code: ‘ECONNREFUSED’,
  errno: ‘ECONNREFUSED’,
  syscall: ‘connect’,
  address: ‘127.0.0.1’,
  port: 30555 }

My node super simple script:

CDP((client) => {
    // extract domains
    // const {Network, Page} = client;
    const Network = client.Network
    const Page = client.Page
    // setup handlers
    Network.requestWillBeSent((params) => {
        console.log(params.request.url);
    });
    Page.loadEventFired(() => {
        client.close();
    });
    // enable events then start!
    Promise.all([
        Network.enable(),
        Page.enable()
    ]).then(() => {
        return Page.navigate({url: 'https://www.something.com/'});
    }).catch((err) => {
        console.error(err);
        client.close();
    });
}).on('error', (err) => {
    // cannot connect to the remote endpoint
    console.error(err);
});

Has anyone had any luck getting this type of thing to work?


回答1:


My Procfile looks like this to first start Chrome, then my Node.js server:

web: /app/.apt/usr/bin/google-chrome & node app/server.js

(Used in Scraping Service, a REST API for scraping dynamic websites. It uses headless Chrome and Cheerio.)




回答2:


Alright I figured it out. When deploying to heroku, I was using two different Procs in the Procfile. One for web which was launching the Node script. And another for launching the headless chrome daemon.

On heroku, those two different procs don't even share the same dyno. Meaning they we're on totally separate "boxes" - at least in theory. This resulted in them having different ports set in the ENVs (not that it even mattered at that point - they might as well be in different continents)

Solution:

Have the node script start the actual headless chrome, then ultimately connect to that child process using the CDP interface.

Also - if you're here and also curious about documentation for the CDP interface for node - it doesnt exist at the moment. Your best option, which is actually pretty good, is: https://chromedevtools.github.io/debugger-protocol-viewer/

Happy hunting.

Edit:

Example of how we handled launching the chrome child process from the application source

const spawn = require('child_process').spawn


spawn('/path/to/chrome/binary',[{`--remote-debugging-port=${process.env.PORT}`]) // Set by heroku
.on('close', () => console.log('CHROME_PROCESS_CLOSE'))
.on('error', e => console.log('CHROME_PROCESS_ERROR', e))
.on('exit', (e, z, a) => console.log('CHROME_PROCESS_EXIT', e, z, a))
.on('data', () => {})


来源:https://stackoverflow.com/questions/43218262/heroku-headless-chrome-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!