在Node中,创建具有“等待连接”超时的http连接的好方法是什么

迪尼斯·克鲁兹(Dinis Cruz)

从单元测试中,我需要创建一个http连接(到Chrome的--remote-debugging-port),并需要一种方法来等待基于http的json服务可用。

我找不到支持这种情况的npm软件包,http://nodejs.org/api/http.html#http_http_request_options_callback似乎不支持连接超时,而我看到的几个示例基于:

  1. 尝试建立与目标网址的连接
  2. 捕获 req.on('error', function (err) {
  3. 检查 err.code === 'ECONNREFUSED'
  4. 以ay ms间隔重试x秒

那是最好的方法吗?

更新1: setTimeout不起作用,因为如果目标URL不可用,我们将收到一个异常

it 'connect_To_Chrome', (done)->
  url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"
  url_Debug.GET (data)->
    assert_Is_Null(data)
    http = require('http')
    # throws error if the value is less that 60 (i.e. 60ms)
    (100).eval_After ->                 # setTimeout
      socket = http.get url_Debug, ->   # create socket
        console.log 'inside get'        # socket was created
      socket.setTimeout 400, ->         # try setTimeout
        console.log 'inside timeout'    # trigered after 400ms
        done()

在上面显示的代码示例中,如果初始超时为10(或根本不使用),我们将获得以下异常

Error: socket hang up
  at createHangUpError (http.js:1476:15)
  at Socket.socketCloseListener (http.js:1526:23)
  at Socket.emit (events.js:117:20)
  at TCP.close (net.js:465:12)

Error: connect ECONNREFUSED
  at errnoException (net.js:904:11)
  at Object.afterConnect [as oncomplete] (net.js:895:19)

更新2:这是解决方案的第一拳

it 'get Chrome Remote Debugging /json', (done)->

  wait_For_Http_GET = (url, timeout, callback)->
    delay = 10;
    try_Http_Get = (next)   =>
      url.GET (data)        => if data is null then (delay).invoke_After next else callback(data)
    run_Tests = (test_Count)=> if test_Count.empty() then url.GET (callback) else try_Http_Get ()->run_Tests(test_Count.splice(0,1))
    run_Tests([0.. ~~(timeout/delay)])

  url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"

  url_Debug.GET (data)->
    assert_Is_Null(data)
    wait_For_Http_GET url_Debug,100, (html)->
      data = JSON.parse(html)
      data.assert_Is_Array().assert_Size_Is(1)
      data[0].description         .assert_Is('')
      data[0].devtoolsFrontendUrl .assert_Is("/devtools/devtools.html?ws=localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
      data[0].id                  .split('-').assert_Size_Is(5)
      data[0].title               .assert_Is('')
      data[0].type                .assert_Is('page')
      data[0].url                 .assert_Is('nw:blank')
      data[0].webSocketDebuggerUrl.assert_Is("ws://localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
      done()
迪尼斯·克鲁兹(Dinis Cruz)

我将选择我的更新2答案,因为它确实按预期工作并回答了问题

  wait_For_Http_GET = (url, timeout, callback)->
    delay = 10;
    try_Http_Get = (next)   =>
      url.GET (data)        => if data is null then (delay).invoke_After next else callback(data)
    run_Tests = (test_Count)=> if test_Count.empty() then url.GET (callback) else try_Http_Get ()->run_Tests(test_Count.splice(0,1))
    run_Tests([0.. ~~(timeout/delay)])

测试

it 'get Chrome Remote Debugging /json', (done)->

  url_Debug = "http://localhost:#{nodeWebKit.port_Debug}/json"

  url_Debug.GET (data)->
    assert_Is_Null(data)
    wait_For_Http_GET url_Debug,100, (html)->
      data = JSON.parse(html)
      data.assert_Is_Array().assert_Size_Is(1)
      data[0].description         .assert_Is('')
      data[0].devtoolsFrontendUrl .assert_Is("/devtools/devtools.html?ws=localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
      data[0].id                  .split('-').assert_Size_Is(5)
      data[0].title               .assert_Is('')
      data[0].type                .assert_Is('page')
      data[0].url                 .assert_Is('nw:blank')
      data[0].webSocketDebuggerUrl.assert_Is("ws://localhost:#{nodeWebKit.port_Debug}/devtools/page/#{data[0].id}")
      done()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

等待连接的正确方法是什么?

“服务器没有响应。连接超时”的影响是什么。Anylogic 中的错误?

在JsRender中创建变量的好方法是什么?

当动态数据确定连接字符串时创建 DbContext 的好模式是什么

什么是http请求期间的连接超时

在graphql模式的连接中具有边和节点的原因是什么?

在项目具有不同时区的列表中实现日期过滤器的好方法是什么?

当多次调用具有多个参数的函数时,python中减少代码重复的好方法是什么?

JDBC中的连接是什么?

ServiceBusTrigger中的连接是什么?

超时等待来自池的连接-尽管有单个SolrServer

在 R 中,按日期连接具有相同列名和维度的数据框并在匹配的列上运行函数的方法是什么

具有客户端认证连接的HTTP客户端的SOAP请求超时异常

具有超时,最大大小和连接池的HTTP请求

删除PostgreSQL中没有直接外键连接的相关记录的最佳方法是什么?

在Pandas面板中连接所有DataFrame的最简单方法是什么?

有损连接分解的补救方法是什么?

Node.js:创建HTTP服务器之前,等待mongodb连接建立

连接超时。为什么?

java.net.SocketTimeoutException中的“连接超时”和“连接失败”之间的确切区别是什么?

在 CloudFormation 中创建 Glue 连接(MONGODB 连接类型)时所需 SSL 的关键是什么

是什么原因导致套接字ConnectException:连接超时?

处理在R中连接的多个表的最佳方法是什么?

在javascript中关闭承诺连接的最干净的方法是什么

在Go中检测丢失的连接的正确方法是什么?

在连接列中搜索的推荐方法是什么?

在 ef core 2 中处理连接的最佳方法是什么

在 Python 中连接字符串 - 最快的方法是什么

从查询的子表中获取连接行的更好方法是什么?