admin管理员组文章数量:1241029
I'm trying to create a real-time website analytics dashboard which creates an open HTTP connection to the server using jQuery/JavaScript asynchronously to poll the server for updates to the data as and when they occur.
The obvious start for this would be to use an XMLHttpRequest
object or jQuery's $.ajax
method to send a GET
or POST
request to the server asynchronously requesting some data.
However, beyond sending one request at a time using a setInterval
method every 30 seconds
I am not sure how to make the connection to the server persistent. Basically, I only want to send one http request and ensure the connection to the server stays open for polling!
My example code with setInterval
is as follows:
<div id="analytics"></div>
<script>
var analytics = document.getElementById('analytics');
setInterval(function(){
$.ajax({ url: "/", success: function(data){
analytics.innerHTML = data;
}, dataType: "json"});
}, 30000);
</script>
I'm trying to create a real-time website analytics dashboard which creates an open HTTP connection to the server using jQuery/JavaScript asynchronously to poll the server for updates to the data as and when they occur.
The obvious start for this would be to use an XMLHttpRequest
object or jQuery's $.ajax
method to send a GET
or POST
request to the server asynchronously requesting some data.
However, beyond sending one request at a time using a setInterval
method every 30 seconds
I am not sure how to make the connection to the server persistent. Basically, I only want to send one http request and ensure the connection to the server stays open for polling!
My example code with setInterval
is as follows:
<div id="analytics"></div>
<script>
var analytics = document.getElementById('analytics');
setInterval(function(){
$.ajax({ url: "http://server./", success: function(data){
analytics.innerHTML = data;
}, dataType: "json"});
}, 30000);
</script>
Share
Improve this question
asked Dec 25, 2012 at 1:31
technojastechnojas
2691 gold badge2 silver badges7 bronze badges
8
- 1 is there any reason why you need to send only one http request? you may want to look into websockets if you want a persistent connection. – kennypu Commented Dec 25, 2012 at 1:35
- http doesn't work that way – charlietfl Commented Dec 25, 2012 at 1:38
- I guess two reasons I want persistence: (1) it will ensure my updates are more real-time and (2) I think its better performance i.e. less thrashing the server with multiple requests? – technojas Commented Dec 25, 2012 at 1:38
- @technojas as charlietfl said, http isn't like tcp connections etc. and it doesn't work that way. Use websockets if you must have a real connection. on the other hand, I don't think there is anything wrong with sending an ajax request frequently, especially every 30 seconds. – kennypu Commented Dec 25, 2012 at 1:39
- 2 if you are doing long polling, you will need special care on your server side (non-blocking i/o), look at socket.io, or research et projects for your server – jermel Commented Dec 25, 2012 at 1:44
1 Answer
Reset to default 12After searching online, this was the answer I was looking for which doesn't use sockets.io
nor WebSockets
but does use jQuery
by taking advantage of its plete
method to create an artificial loop:
<div id="analytics"></div>
<script>
var analytics = document.getElementById('analytics');
(function poll(){
$.ajax({ url: "server", success: function(data){
analytics.innerHTML = data;
}, dataType: "json", plete: poll, timeout: 30000 });
})();
</script>
Source is Tian Davis from Technoctave: http://techoctave./c7/posts/60-simple-long-polling-example-with-javascript-and-jquery
本文标签: Simple long polling example with JavaScript and jQueryStack Overflow
版权声明:本文标题:Simple long polling example with JavaScript and jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740102338a2224657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论