[英]How to check if a website is up asynchronously?
我现在有两种方法,第一种是通过检查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
在第二个站点中,我要做的是从我拥有的一系列站点中选择一个随机站点,并在第一个方法的帮助下验证该站点是否正常。 如果是的话,我想将该站点作为父方法的返回值(在本例中为第二个),但是如果不是,我想重新运行该方法,直到它在数组中找到一个在线站点。 到目前为止,这是我所做的:
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
显然,这不是我想要的方式:如果状态码不是200,那么它实际上会重新运行该方法(到目前为止,我们还可以); 但是,当站点确实在线时,问题就出现了,因为我不知道如何将selected_site变量(在checkifUp调用中声明)作为父方法的返回值 (在本例中为siteSet() )返回。 我需要这样做,以便能够将siteSet()返回值用作另一个函数的变量:
otherFunc = ->
theSite = siteSet()
console.log(theSite)
Javascript: http : //goo.gl/cmsryJ
并且确信它将始终设置为thisotherFunc()中的在线站点URL(字符串)
我对此有两个问题 :
我该如何完成我想在这里做什么? (Du,那是很明显的呵呵)
我对此不太确定,但是据我了解Javascript / Coffeescript,当从otherFunc()中调用siteSet()时(至少使用此“设置”),otherFunc()不会等到siteSet()返回一个字符串(这是我想要的结果)我正确吗? 即使解决了返回问题,我认为也会发生的是,当我从otherFunc()中调用siteSet()时 ,它将使用调用的确切结果,这意味着,如果运行siteSet()时,它将返回另一个对其自身进行调用 (因为所选的random_site不在在线),otherFunc()中的“ theSite”变量将使用裸函数()作为值,对吗? (如果是这种情况),如何解决另一个问题? 我想在otherFunc()内设置“ theSite”变量, 直到该值是我需要的String 为止 。
在此先感谢您的帮助。
这里的问题是,您要对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()
结果将是这样的:
resultA is undefined
calling from funB
calling from funA
您的代码将被翻译为:
//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
//
为了更好地理解为什么nodejs以这种方式执行代码,请查看以下文章...
http://docs.nodejitsu.com/articles/getting-started/control-flow/what-are-callbacks
http://dreamerslab.com/blog/en/javascript-callbacks/
x = LONGIO()
console.log(x)
vs
LONGIO((resultOfLongIO)-> console.log(resultOfLongIO))
从根本上讲,异步代码(没有promise,generators,monads或任何东西)中的思想是,将函数A的结果传递给回调函数……就这样了……
我不太了解CoffeeScript,因此我正在将JavaScript转换用于您的两个函数。 如果我不知道CoffeeScript中有某些特质,请告诉我。
也就是说,您似乎正在使用异步函数siteSet,并尝试同步返回值。 您可以检查此问题的解决方案以获取更多信息。 您有两种方法可以有效地返回值:
您对第一个函数checkIfUp有一个回调,所以我的建议是也对siteSet使用一个回调。 您可以调用如下:
otherFunc = ->
theSite = siteSet ( theSite ) ->
console.log(theSite)
至少,我认为这是CoffeeScript语法。 在JavaScript中,绝对是:
otherFunc()
{
theSite = siteSet( function( theSite )
{
console.log( theSite );
} );
}
就像对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)
然后,您可以执行以下操作:
otherFunc = ->
siteSet (theSite) ->
console.log theSite
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.