1

我正面临 NodeJS 的问题。最初,我需要大约 4-5 秒才能获得任何更新。Node JS 服务器是可公开访问的,它不通过任何代理或任何东西进行修补。但是一旦建立了初始连接 - 更新是即时的。

我使用 Chrome 的网络工具深入挖掘 - 它说它正在等待数据。见附图

我还粘贴了我的 app.js(节点应用程序)的代码供您参考。

var http = require('http'),
    url = require('url'),
    fs = require('fs'),
    amqp = require('amqp'),
    sys = require(process.binding('natives').util ? 'util' : 'sys');
var exchangeName = 'conferenceTest';
send404 = function (res) {
  res.writeHead(404);
  res.write('404');
  res.end();
};
server = http.createServer(function (req, res) {
  var path = url.parse(req.url).pathname;
  switch (path) {
  case '/':
    fs.readFile(__dirname + "/index.html", function (err, data) {
      if (err) {
        return send404(res);
      } else {
        res.writeHead(200, {
          'Content-Type': 'application/zip',
          'Connection': 'close',
          'content-encoding': 'gzip'
        });
        res.write(data, 'utf8');
        res.end();
      }
    });
    break;
  }
});
// listen to the http server for socket connections
var io = require('socket.io').listen(server);
var connection = amqp.createConnection({
  host: 'localhost'
});
connection.on('ready', function () {
  var exchange = connection.exchange(exchangeName, { // create exchange
    type: 'direct',
    durable: true
  });
  io.sockets.on('connection', function (client) {
    console.log("client connected");
    client.on('changeview', function (data) {
      var queue = connection.queue(data.queueName, { //create queue
        durable: true,
        autoDelete: false
      });
      var plaintext = "Put any kind of meat on a stick and roast it over a flame and it immediately becomes food fit for gods. No country understands this sacred rule of seared meat like Turkey.Turkish kebabs are the incarnation of the meat lovers most exotic fantasies, with grilled lamb, beef and chicken as skewer MVPs.Most kebab restaurants also have a long list of Turkish starters called meze that are as delicious as the main dishes.Turkeys best alcoholic complement for all that meat is raki -- an aniseed-flavored drink that s often diluted with water and chilled with ice. Frothy, yogurt-based ayran is a great non-alcoholic complement to heavy dishes. But who are we kidding -- you just want the meat. Heres where to get it in Turkey.";
      io.sockets.emit('changeview', plaintext);
    });
  });
});
process.on('uncaughtException', function (err) {
  console.log('Uncaught Exception: ' + err.message);
});
server.listen(18080);

谢谢

在此处输入图像描述

4

2 回答 2

1

您的客户端正在请求一些长的数字 URI,但您的处理程序只接受/(它发回的index.html)请求。其他请求(如数字 URI)根本不处理,这意味着您的浏览器将等待一段时间并最终放弃。

要解决,请添加默认情况并返回404错误:

switch (path) {
  case '/':
    // your current code
    break;
  default:
    send404(res);
}

另外:为什么要在index.html文件的响应中设置这些特定的标头?application/zip意味着您index.html应该被视为一个 ZIP 文件,并且将内容编码设置为gzip意味着响应是 gzip'ed。这里似乎都不是这种情况。

于 2013-05-16T07:44:49.737 回答
0

客户端- 默认连接超时为 10 秒

我已将它减少到 500 毫秒 - 它现在几乎在 2.92 秒内连接 - 以前最多需要 12.3 秒

var conn = io.connect("http://myserver.com:myport/", { transports: transports, 'reconnect': true, 'connect timeout':500});

希望它可以帮助其他在这个问题上苦苦挣扎的人

于 2013-05-20T07:46:38.343 回答