Heroku - 无头Chrome - 连接拒绝 [英] Heroku - Headless Chrome - Connection Refused

查看:190
本文介绍了Heroku - 无头Chrome - 连接拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用:Heroku Build Pack for headless chrome。
https://github.com/heroku/heroku-buildpack-google -chrome /



我遇到了这个令人生厌的错误,我的节点脚本(show below)无法连接到chrome实例。我得到一个非常明确的错误:

  {错误:在Object.exports上连接ECONNREFUSED 127.0.0.1:30555 
._errnoException(util.js:1018:11)
at exports._exceptionWithHostPort(util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete](net.js:1090:14)
代码:'ECONNREFUSED',
errno:'ECONNREFUSED',
syscall:'connect',
地址:'127.0.0.1',
端口号:30555}

我的节点超简单脚本:

<$ p $ CDP((客户端)=> {
//提取域
//常量{网络,页面} =客户端;
常量网络=客户端。网络
const Page = client.Page
//设置处理程序
Network.requestWillBeSent((params)=> {
console.log(params.request.url);
$ b});
Page.loadEventFired(()=> {
client.close();
});
//启用事件然后开始!
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)=> {
//无法连接到远程端点
console.error(err);
});

有没有人有幸运过这种类型的东西?

解决方案

好吧,我明白了。在部署到heroku时,我在Procfile中使用了两个不同的Proc。一个用于启动节点脚本的 web 。另一个用于启动无头chrome守护进程。



在heroku上,这两个不同的proc甚至不共享同一个dyno。意思是他们我们完全分开的盒子 - 至少在理论上。这导致他们在ENVs中设置了不同的端口(并不是说它在那个时候甚至是重要的 - 他们可能在不同的大陆)。

解决方案:



使节点脚本启动实际的无头镶嵌,然后使用 CDP 接口最终连接到该子进程。



另外 - 如果您在这里并且对 CDP 节点的文档感兴趣,它现在并不存在。你最好的选择,其实很不错,它是: https://chromedevtools.github.io / debugger-protocol-viewer /

快乐狩猎。

编辑:



我们如何处理从应用程序源启动chrome子进程的示例

  const spawn 

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


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?

解决方案

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', () => {})

这篇关于Heroku - 无头Chrome - 连接拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆