【问题标题】:Electron net module is taking too long to get a response电子网模块需要很长时间才能得到响应
【发布时间】:2019-04-04 19:08:21
【问题描述】:

我正在尝试发送一个带有数据的 POST http 请求,但是花费的时间太长了!所以我遵循了电子文档相同的示例代码,结果也很慢。 “响应”事件大约需要 40 秒才能触发并返回数据!!

来自https://electronjs.org/docs/api/net的示例代码

const { app } = require('electron')
app.on('ready', () => {
  const { net } = require('electron')
  const request = net.request('https://github.com')
  request.on('response', (response) => {
    console.log(`STATUS: ${response.statusCode}`)
    console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
    response.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`)
    })
    response.on('end', () => {
      console.log('No more data in response.')
    })
  })
  request.end()
})

我使用了 nodejs http 模块,它工作正常,但我想知道为什么 Electron 的原生模块需要这么长时间才能返回结果?

【问题讨论】:

  • 无法弄清楚您链接的解决方案与我的电子网络模块速度慢有何关系?
  • net 模块使用 Chromium 的原生网络库。所以它类似于使用浏览器。我测试了你的代码,我得到了非常快的响应。你在使用代理吗?
  • 请检查这是否与您的问题有关:github.com/electron/electron/issues/13829
  • 我也看到了,我运行的设置与提出问题的设置不同(windwos 7 with virtual box),但它与我得到的缓慢结果相差无几:/

标签: javascript http electron


【解决方案1】:

我也遇到过同样的问题。在我的情况下,我的公司使用代理,所以我的请求花费了太多时间(在你的情况下大约 40 秒)。我的解决方案是使用 Node HTTP 请求(与 axios 一起使用 proxy: false)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多