使用 node.js 和名为“echo”的 sockjs github 示例https://github.com/sockjs/sockjs-node,没有任何更改,传输在大约 130-150 秒内关闭,如下图所示。在尝试了不同速率的心跳之后,在这些事情上有一些经验的人有什么想法为什么我需要这么低的心跳率?还是我错过了一些基本的东西?或者实际上有人知道这是生产环境的正常速率吗?服务器距离客户端 204 毫秒,尝试了 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>