0

I am new to node, so please bear with me. I'm trying to write a function that tests to make sure there is a live connection to the web server before redirecting the page.

This works for the first 6 - 7 clicks, then the page will not redirect anymore - it just sits there. Then a few minutes later, an alert will show.

What is going on?!

var http = require("http");
var url = 'http://example.com/';

mainMenu.click(function () {
  var menulink = $(this).attr('rel');
  var menuvar = http.get(url, function () {
    window.location = menulink;
  }).on('error', function () {
    alert('Cannot Connect to Server');
  });
});
4

1 回答 1

0

我怀疑您对非流动流有疑问。

由于您从不使用响应数据,因此连接永远不会关闭。HTTP 代理限制并发连接的数量,并在一段时间后拒绝打开任何新连接。

您应该尝试手动将响应切换到流动模式:

mainMenu.click(function () {
  var menulink = $(this).attr('rel');
  var menuvar = http.get(url, function (res) {
    // FORCE FLOWING MODE
    res.resume();
    window.location = menulink;
  }).on('error', function () {
    alert('Cannot Connect to Server');
  });
});
于 2013-09-27T18:05:27.663 回答