node.js - sockjs 140秒后关闭

标签 node.js sockjs

使用 node.js 和名为“echo”的 sockjs github 示例 https://github.com/sockjs/sockjs-node ,如果没有任何变化,传输将在大约 130-150 秒内关闭,如下图所示。在尝试不同速率的心跳后,在这些方面有一定经验的人是否知道为什么我需要这么低的心跳率?或者我错过了一些基本的东西?或者实际上有人知道这是生产环境的正常速率吗? 服务器距离客户端204ms,尝试端口8080和442。

设置:

HTTP, Ubuntu 12.04, Linode VPS

进行一些小的更改以添加检测信号会产生较差的结果,这表明 Chrome 需要大约 5 秒的检测信号,而 IE 则需要 3 秒的检测信号。 关键:unlim 显示积极结果 - 运输在测试时间结束 20 多分钟之前没有关闭。破折号显示未测试

Browser:Chrome  Trans:websocket
------------------------------------------
sec per beat    Closed (Port 8080)      Closed (Port 442)
30              150 s                       150 s
25              150 s                       150 s
15              135 s                       135 s
12              unlim                       130 s
10              130 s                       150 s
8               unlim                       unlim
6               unlim                       unlim   
5               unlim                       -
3               unlim                       -


Browser:IE-10  Trans:xhr-stream 
------------------------------------------
sec per beat    Closed (Port 8080)      Closed (Port 442)
30              180 s                       -
25              175 s                       -
15              150 s                       -
12              156 s                       -
10              150 s                       -
8               144 s                       -
6               90 s                        -
5               270 s                       256 s
3               unlim                       unlim

这是 sockjs 示例中的原始服务器代码,仅更改了端口:

var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');

// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
    conn.on('data', function(message) {
        conn.write(message);
    });
});

// 2. Static files server
var static_directory = new node_static.Server(__dirname);

// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
    static_directory.serve(req, res);
});
server.addListener('upgrade', function(req,res){
    res.end();
});

sockjs_echo.installHandlers(server, {prefix:'/echo'});

console.log(' [*] Listening on 0.0.0.0:442' );
server.listen(442, '0.0.0.0');

这里是 sockjs 示例中的原始客户端代码(代码中注明了更改),基本上将 inp.val() x 1000 作为轻松测试的节拍

<!doctype html>
<html><head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
    <style>
      .box {
          width: 300px;
          float: left;
          margin: 0 20px 0 20px;
      }
      .box div, .box input {
          border: 1px solid;
          -moz-border-radius: 4px;
          border-radius: 4px;
          width: 100%;
          padding: 0px;
          margin: 5px;
      }
      .box div {
          border-color: grey;
          height: 300px;
          overflow: auto;
      }
      .box input {
          height: 30px;
      }
      h1 {
          margin-left: 30px;
      }
      body {
          background-color: #F0F0F0;
          font-family: "Arial";
      }
    </style>
</head><body lang="en">
    <h1>SockJS Echo example</h1>

    <div id="first" class="box">
      <div></div>
      <form><input autocomplete="off" value=""></input></form>
    </div>

    <script>
        var sockjs_url = 'http://games.the-checkout-tech.com:442/echo';
        var sockjs = new SockJS(sockjs_url);
        $('#first input').focus();

        var div  = $('#first div');
        var inp  = $('#first input');
        var form = $('#first form');

        var print = function(m, p) {
            p = (p === undefined) ? '' : JSON.stringify(p);
            div.append($("<code>").text(m + ' ' + p));
            div.append($("<br>"));
            div.scrollTop(div.scrollTop()+10000);
        };

        sockjs.onopen    = function()  {print('[*] open', sockjs.protocol);};
        sockjs.onmessage = function(e) {print('[.] ', e.data); };
        sockjs.onclose   = function()  {print('[*] close');};

        form.submit(function() {
            print('[ ] sending', inp.val());
            // sockjs.send(inp.val());          // change
            startBeat();                        // change
            return false;
        });

        /******* Changes below ********/
        function startBeat() {
            window.setInterval(function() {
                sockjs.send(getTime());
            }, inp.val() * 1000);   
        }

        function getTime() {
            var d = new Date();
            output = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
            return output;
        }

    </script>
</body></html>

最佳答案

似乎在端口 80 上使用 Apache 提供 index.html 文件并在端口 8080 上与 sockjs(以及与此相关的 socket.io)通信是一个坏主意。 - 不知道为什么。

通过让 Node 静态提供文件,将 Apache 排除在外,问题得到了彻底解决。

var node_static = require('node-static');

// 2. Static files server
var static_directory = new node_static.Server(__dirname);

关于node.js - sockjs 140秒后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870995/

相关文章:

node.js - MongoDB 返回空数组

node.js - locomotivejs 和 Mongoose : passing promise variables to a controller

java - 具有负载均衡器回退功能的 Spring Websocket

websocket - sockjs-tornado 方法中的异步调用

python - Vertx SockJS 服务器与 sockjs-tornado

python - Tornado 中的 Django 用户

javascript - 带有异步/等待的 super 代理/ super 测试

javascript - Grunt less 文件观察器

java - 如何使用 spring+stomp+sockjs 获取 websocket 响应

Node.js/Redis - 等待连接?