使用node.js和名为'echo‘https://github.com/sockjs/sockjs-node的sockjs github示例,在没有任何更改的情况下,传输在大约130-150秒内关闭,如下图所示。在尝试了不同频率的心跳后,有经验的人知道为什么我需要这么低的心跳频率吗?还是我遗漏了一些基本的东西?或者,实际上是否有人知道这是生产环境的正常速率?服务器与客户端的距离为204ms,已尝试端口8080和442。
设置:
HTTP, Ubuntu 12.04, Linode VPS做一些小改动来添加心跳会得到这些糟糕的结果,这表明Chrome需要大约5秒的心跳,IE需要3秒的心跳。关键: unlim显示肯定的结果-在测试时间20+分钟结束之前传输没有关闭。dash显示未测试
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() x1000作为节拍,以便于测试
<!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>发布于 2013-09-21 12:35:05
在端口80上与Apache一起提供index.html文件,并在端口8080上与sockjs (以及socket.io )对话似乎不是一个好主意。-不知道为什么.
通过让节点静态地为文件提供服务,这个问题被完全修复了,而Apache则不在考虑范围之内。
var node_static = require('node-static');
// 2. Static files server
var static_directory = new node_static.Server(__dirname);https://stackoverflow.com/questions/18870995
复制相似问题