Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

electron 中 http 和node不一样,总是获取不完整

这个是node中的代码
`

const https = require("https");

const url = "https://www.baidu.com/";
var req = https.request(url, function (response) {
    var chunks = [];
    response.on("data", function (chunk) {
        console.log("===================================");
        chunks.push(chunk);
    });
    response.on("end", function () {
        var buffer = Buffer.concat(chunks);
        console.log(buffer.toString());
    });
});

req.on("error", function (e) {
    console.log(e.message);
});
req.end();

`

这个可以获取完整的html, 但是吧这个代码贴进 electron 就中不行了, 获取的html不完整并且还乱码。
请问是怎么回事?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

写完才发现是个挖坟贴....

这种情况你应该拿出一个能复现你的问题的“最简”代码放在github或什么地方能让人查看,否则鬼知道你遇到的是什么问题?

直觉上我并不觉得electron这种迭代很久的产品会有这样明显的功能性问题,今天无事索性用个demo验证一下

main.js

const { app, BrowserWindow } = require("electron");
const path = require("path");
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
app.whenReady().then(() => {
  const window = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
    },
  });
  window.loadFile(path.join(__dirname, "index.html"));
});
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

package.json

{
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "dependencies": {
    "electron": "^10.0.0"
  }
}

index.html

<bdoy></bdoy>
<script src="./app.js"></script>

app.js

const https = require("https");
function request(url, time) {
  return new Promise((resolve, reject) => {
    const req = https.request(url, (res) => {
      const chunks = [];
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });
      res.on("end", function () {
        const html = Buffer.concat(chunks).toString();
        console.log("  " + html.substring(html.length - 9));
        console.log(`====end:  ${time}====`);
        resolve();
      });
    });
    req.end();
    console.log(`====start:${time}====`);
  });
}

(async function () {
  for (let i = 0; i < 5; i++) {
    await request("https://www.baidu.com/", i + 1);
  }
})();

第一次输出没有任何问题

====start:1====
app.js:11   </html>

app.js:12 ====end:  1====
app.js:17 ====start:2====
app.js:11   </html>

app.js:12 ====end:  2====
app.js:17 ====start:3====
app.js:11   </html>

app.js:12 ====end:  3====
app.js:17 ====start:4====
app.js:11   </html>

app.js:12 ====end:  4====
app.js:17 ====start:5====
app.js:11   </html>

app.js:12 ====end:  5====

考虑到用脚手架开发都会动态刷新,可能是刷新的问题? 我F5了之后确实有问题,现在输出只有:

====start:1====

看起来是事件没有触发,用关键词electron node async io not working on reload搜索,第一条就是:https://github.com/electron/electron/issues/22119,看https://github.com/electron/electron/issues/18397里面的时间线一时半会还没法确定。

不过简单的把主进程添加`app.allowRendererProcessReuse = false可以解决上面的问题,只不过楼主遇到的问题是不是这个就不知道了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...