我正在尝试连接到 Chromium in the same way Puppeteer does in NodeJS .
这看起来 super simple in NodeJS .您向 stdio 数组再添加两个参数,您就拥有了管道。
我无法在 Puppeteer-Sharp 中实现相同的逻辑.我花了一些时间在这里阅读了很多问题和答案。我阅读了有关 AnonymousPipeServerStream 的信息,但并不高兴。
这是一个我无法让它工作的例子:
AnonymousPipeServerStream streamReader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
AnonymousPipeServerStream streamWriter = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
var chromeProcess = new Process();
chromeProcess.EnableRaisingEvents = true;
chromeProcess.StartInfo.UseShellExecute = false;
chromeProcess.StartInfo.FileName = "/.local-chromium/MacOS-536395/chrome-mac/Chromium.app/Contents/MacOS/Chromium";
chromeProcess.StartInfo.Arguments =
"--MANY-MANY-ARGUMENTS " +
"--remote-debugging-pipe " +
"--user-data-dir=/var/folders/0k/4qzqprl541b74ddz4wwj_ph40000gn/T/mz0trgjc.vlj " +
"--no-sandbox " +
"--disable-dev-shm-usage " +
streamReader.GetClientHandleAsString() +
streamWriter.GetClientHandleAsString();
chromeProcess.Start();
streamReader.DisposeLocalCopyOfClientHandle();
streamWriter.DisposeLocalCopyOfClientHandle();
Task task = Task.Factory.StartNew(async () =>
{
var reader = new StreamReader(streamReader);
while (true)
{
var response = await reader.ReadToEndAsync();
if (!string.IsNullOrEmpty(response))
{
Console.WriteLine(response);
}
}
});
Console.ReadLine();
许多示例表明您必须将 GetClientHandleAsString()
作为参数传递,但我看不出它如何连接到进程。
这是 the full example 的要点
最佳答案
答案在于启动 Process
用ProcessStartInfo
有 RedirectStandardInput
, RedirectStandardOutput
和/或 RedirectStandardError
属性设置,然后使用 Process.StandardInput
, Process.StandardOutput
和/或 Process.StandardError
访问管道的属性。
关于C# 进程 : Using Pipes/File Descriptors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938404/