简体   繁体   English

如何检查网站是否异步启动?

[英]How to check if a website is up asynchronously?

I have two methods I'm using right now, the first one is to check if a website is up or not, by checking the HTTP GET status code response: 我现在有两种方法,第一种是通过检查HTTP GET状态代码响应来检查网站是否正常运行:

checkifUp = (host, callback) ->
  host = host.replace("http://", "").replace("/", "")
  options =
    host: "#{host}"
    port: 80
    path: "/"

  req = http.get(options, (res) ->
    callback(res.statusCode.toString())
    req.connection.destroy()
  )

Javascript: http://goo.gl/OyekSx Javascript: http //goo.gl/OyekSx

And with the second one, what I do is to select a random site from an array of websites I have and verify (with the help of the first method) if such site is up. 在第二个站点中,我要做的是从我拥有的一系列站点中选择一个随机站点,并在第一个方法的帮助下验证该站点是否正常。 If it is, I wanna return the site as the return value of the parent method (in this case the second one), but if it is not, I wanna re-run the method until it finds an online site in the array. 如果是的话,我想将该站点作为父方法的返回值(在本例中为第二个),但是如果不是,我想重新运行该方法,直到它在数组中找到一个在线站点。 This is what I have done so far: 到目前为止,这是我所做的:

siteSet = ->
  site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com", "sub4.mysite.com", "sub5.mysite.com"]
  random_site = site_urls[Math.floor(Math.random()*site_urls.length)]

  checkifUp "#{random_site}", (code) ->
    if code isnt "200"
      siteSet()
    else
      selected_site = random_site
  selected_site

Javascript: http://goo.gl/ydmSiV Javascript: http //goo.gl/ydmSiV

Obviously, this doesn't work the way I want: if the status code isn't 200, then it re-runs the method indeed, (so far, we're ok); 显然,这不是我想要的方式:如果状态码不是200,那么它实际上会重新运行该方法(到目前为止,我们还可以); But the problems appear when the site is indeed online as I don't know how to return the selected_site variable (declared inside the checkifUp call) as the return value of the parent method (in this case siteSet() ). 但是,当站点确实在线时,问题就出现了,因为我不知道如何将selected_site变量(在checkifUp调用中声明)作为父方法的返回值 (在本例中为siteSet() )返回。 I need to do this in order to be able to use the siteSet() return value as a variable for another function: 我需要这样做,以便能够将siteSet()返回值用作另一个函数的变量:

otherFunc = ->
  theSite = siteSet()
  console.log(theSite)

Javascript: http://goo.gl/cmsryJ Javascript: http //goo.gl/cmsryJ

And be confident it will always be seted to an online site URL (string) inside this otherFunc() 并且确信它将始终设置为thisotherFunc()中的在线站点URL(字符串

I have two questions about this: 我对此有两个问题

  1. How can I accomplish what I wanna do here? 我该如何完成我想在这里做什么? (Duh, that one it's obvious hehe) (Du,那是很明显的呵呵)

  2. I'm not quite sure about this, but as far as I understand Javascript/Coffeescript, when siteSet() gets called from within otherFunc() , (at least with this "setup"), the otherFunc() won't wait until siteSet() returns a String (which is the result I want) am I correct? 我对此不太确定,但是据我了解Javascript / Coffeescript,当从otherFunc()中调用siteSet()时(至少使用此“设置”),otherFunc()不会等到siteSet()返回一个字符串(这是我想要的结果)我正确吗? Even with the return problem solved, what I think is going to happen is that when I call siteSet() from within otherFunc() it will use the exact result from the calling, this means that if when siteSet() gets runned it returns another call to itself (because the random_site chosen was not online) the "theSite" variable inside otherFunc() will take the bare function() as value, am I correct? 即使解决了返回问题,我认为也会发生的是,当我从otherFunc()中调用siteSet()时 ,它将使用调用的确切结果,这意味着,如果运行siteSet()时,它将返回另一个对其自身进行调用 (因为所选的random_site不在在线),otherFunc()中的“ theSite”变量将使用裸函数()作为值,对吗? (and if that's the case), how to solve this other problem? (如果是这种情况),如何解决另一个问题? I want to set the "theSite" variable inside of otherFunc() until such value is a String as I need it to. 我想在otherFunc()内设置“ theSite”变量, 直到该值是我需要的String 为止

Thanks in advance for any help. 在此先感谢您的帮助。

the problem here is than you're taking a sync approach for your otherFunc instead of the async one, let's ckeck this: 这里的问题是,您要对otherFunc采取同步方法而不是异步方法,让我们来看看:

   //sorry I prefer use plain js, I'm pretty sure than you will be able to understand the code
   var funA = function(){
   //long computation here
    console.log("calling from funA");

   }

 var funB = function(){
    var resultA = funA();
    console.log("resultA is " + resultA);
    console.log("calling from funB");

  }

  funB()

the result would be something like this: 结果将是这样的:

     resultA is undefined
     calling from funB
     calling from funA

your code would be translate to this: 您的代码将被翻译为:

  //sorry I'm not so familiar with coffeescript so maybe I would do a little mistake
  siteSet = (callback)->
    site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com",           "sub4.mysite.com", "sub5.mysite.com"]
    random_site = site_urls[Math.floor(Math.random()*site_urls.length)]

    checkifUp "#{random_site}", (code) ->
      if code isnt "200"
         siteSet()
      else
      selected_site = random_site

      callback(selected_site)


   otherFunc = ->
       siteSet((result)-> console.log(result))  //(result)-> console.log(result) is your
                                            //callback, so inside checkifUp you will call it and pass selected_site
                                            //

for better understand of why nodejs execute the code in this way check these articles... 为了更好地理解为什么nodejs以这种方式执行代码,请查看以下文章...

http://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks http://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks

http://dreamerslab.com/blog/en/javascript-callbacks/ http://dreamerslab.com/blog/en/javascript-callbacks/

    x = LONGIO()
    console.log(x)

    vs

    LONGIO((resultOfLongIO)-> console.log(resultOfLongIO))

basically the idea in async code (without promise,generators,monads or whatever) is than you pass the result of your functionA to the callback...that is all... 从根本上讲,异步代码(没有promise,generators,monads或任何东西)中的思想是,将函数A的结果传递给回调函数……就这样了……

I don't know CoffeeScript that well, so I'm using the javascript conversions for your two functions. 我不太了解CoffeeScript,因此我正在将JavaScript转换用于您的两个函数。 If there are idiosyncrasies in CoffeeScript that I'm not aware of, please let me know. 如果我不知道CoffeeScript中有某些特质,请告诉我。

That said, it looks like you're using an asynchronous function, siteSet, and trying to return a value synchronously. 也就是说,您似乎正在使用异步函数siteSet,并尝试同步返回值。 You can check the solutions to this question for more information. 您可以检查此问题的解决方案以获取更多信息。 You have two options for returning values effectively: 您有两种方法可以有效地返回值:

  • Events 活动
  • Callbacks 回调

You have a callback for your first function, checkIfUp, so my recommendation is to use one for siteSet as well. 您对第一个函数checkIfUp有一个回调,所以我的建议是也对siteSet使用一个回调。 You could invoke as follows: 您可以调用如下:

otherFunc = ->
  theSite = siteSet ( theSite ) ->
    console.log(theSite)

At least, I think that's the CoffeeScript syntax. 至少,我认为这是CoffeeScript语法。 In JavaScript it's definitely: 在JavaScript中,绝对是:

otherFunc() 
{
  theSite = siteSet( function( theSite )
  {
    console.log( theSite );
  } );
}

Add a callback to siteSet just like you did with checkifUp: 就像对checkifUp一样,将回调添加到siteSet中:

siteSet = (callback) ->
  site_urls = ["sub1.mysite.com", "sub2.mysite.com", "sub3.mysite.com", "sub4.mysite.com", "sub5.mysite.com"]
  random_site = site_urls[Math.floor(Math.random()*site_urls.length)]

  checkifUp "#{random_site}", (code) ->
    if code isnt "200"
      siteSet()
    else
      callback(random_site)

Then you can do: 然后,您可以执行以下操作:

otherFunc = ->
  siteSet (theSite) ->
    console.log theSite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM