问题描述
我有两个用于操纵p的自动化的Node.js脚本.
I have two Node.js scripts for puppeteer automation.
1)launcher.js
1) launcher.js
此Puppeteer脚本启动chrome浏览器并断开chrome的连接,以便可以使用WSEndpoint进行连接.
This Puppeteer script launches a chrome browser and disconnects the chrome so that it can be connected by using WSEndpoint.
const puppeteer = require('puppeteer');
module.exports = async () => {
try {
const options = {
headless: false,
devtools: false,
ignoreHTTPSErrors: true,
args: [
`--no-sandbox`,
`--disable-setuid-sandbox`,
`--ignore-certificate-errors`
]
};
const browser = await puppeteer.launch(options);
let pagesCount = await browser.pages();
const browserWSEndpoint = await browser.wsEndpoint();
// console WSEndPoint say : "ws://127.0.0.1:42207/devtools/browser/dbb2525b-ce44-43c2-a335-ff15d0306f36"
console.log("browserWSEndpoint----- :> ", browserWSEndpoint);
await browser.disconnect();
return browserWSEndpoint;
} catch (err) {
console.error(err);
process.exit(1);
return false;
}
};
2)connector.js
2) connector.js
启动无头chrome,并尝试使用各种主机名通过WSEndPoint连接chrome.如果我使用运行命令node connector.js localhost
来运行此脚本,它将尝试将WSEndpint与localhost作为主机名连接.
Launches headless chrome and tries to connect chrome by WSEndPoint by various hostnames. If I ran this script with run command as node connector.js localhost
, it tries to connect WSEndpint with localhost as the hostname.
const puppeteer = require('puppeteer');
const launcher = require('./launcher');
(async (host) => {
try {
let WSEndPoint = await launcher();
WSEndPoint = WSEndPoint.replace('127.0.0.1', host);
console.log("WSENDPOINT :", WSEndPoint);
const browser = await puppeteer.connect({
browserWSEndpoint: WSEndPoint,
ignoreHTTPSErrors: true
});
let pagesCount = await browser.pages();
console.log("Pages available :> ", pagesCount.length);
// const browserWSEndpoint = await browser.wsEndpoint();
await browser.disconnect();
process.exit(1);
return true;
} catch (err) {
console.error(err);
process.exit(1);
return false;
}
})(process.argv[2]);
但是我无法使用本地IP地址(例如192.168.1.36)连接chrome的WSEndpint.为什么?
But I can't connect the WSEndpint of the chrome using my local IP address ( say 192.168.1.36). Why?
错误消息是
{ Error: connect ECONNREFUSED 192.168.1.33:36693
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.1.33',
port: 36693 }
推荐答案
您可以代理websocket并连接到代理.我在服务器上对其进行了测试,并成功运行了.
You can proxy the websocket and connect to the proxy instead. I tested it on my server and it ran successfully.
这是我们使用http-proxy
,
const httpProxy = require("http-proxy");
const host = "0.0.0.0";
const port = 8080;
async function createServer(WSEndPoint, host, port) {
await httpProxy
.createServer({
target: WSEndPoint, // where we are connecting
ws: true,
localAddress: host // where to bind the proxy
})
.listen(port); // which port the proxy should listen to
return `ws://${host}:${port}`; // ie: ws://123.123.123.123:8080
}
然后,当我们运行它时,我们创建一个代理服务器并监听ws端点.为了简化说明,我不会导出它.
Then when we run it, we create a proxy server and listen to the ws endpoint instead. I am not exporting it to keep the explanation simpler.
// your other codes
const pagesCount = (await browser.pages()).length; // just to make sure we have the same stuff on both place
const browserWSEndpoint = await browser.wsEndpoint();
const customWSEndpoint = await createServer(browserWSEndpoint, host, port); // create the server here
console.log({ browserWSEndpoint, customWSEndpoint, pagesCount });
// your other code here
当我在服务器上运行时,我们得到以下信息,
When I run on the server, we get the following,
在小滴上,
➜ node index.js
{ browserWSEndpoint: 'ws://127.0.0.1:45722/devtools/browser/df0ca6a9-48ba-4962-9a20-a3a536d403fa',
customWSEndpoint: 'ws://0.0.0.0:8080',
pagesCount: 1 }
客户
然后我们像这样连接它,
Client
Then we connect it like this,
const puppeteer = require("puppeteer-core");
(async serverAddr => {
const browser = await puppeteer.connect({
browserWSEndpoint: `ws://${serverAddr}`,
ignoreHTTPSErrors: true
});
const pagesCount = (await browser.pages()).length;
const browserWSEndpoint = await browser.wsEndpoint();
console.log({ browserWSEndpoint, pagesCount });
})(process.argv[2]);
在我的访客计算机上,
➜ node index.js "123.123.123.123:8080"
Pages available :> 1
{ browserWSEndpoint: 'ws://123.123.123.123:8080' }
这样,我们可以在多台服务器上托管并在它们之间路由,扩展以及在需要时进行更多扩展.
This way we can host on multiple server and route between them, scale away and more if needed.
和平!
这篇关于木偶-如何使用本地IP地址连接WSEndpoint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!