admin管理员组文章数量:1336136
I created my own chat webserver in python and wanted to know instead of AJAX making a call to the server every second (JS below). I can modify my server so when ever it updates the chat.html file it pushes it out to all the clients. Is there a way using javascript to have it listen for any received data rather then polling?
<script>
// Request the AJAX update the chat window every second
setInterval(function(){loadChat()},1000);
function loadChat()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatWindow").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","chat.html",true);
xmlhttp.send(null);
}
}
</script>
I created my own chat webserver in python and wanted to know instead of AJAX making a call to the server every second (JS below). I can modify my server so when ever it updates the chat.html file it pushes it out to all the clients. Is there a way using javascript to have it listen for any received data rather then polling?
<script>
// Request the AJAX update the chat window every second
setInterval(function(){loadChat()},1000);
function loadChat()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("chatWindow").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","chat.html",true);
xmlhttp.send(null);
}
}
</script>
Share
edited Jun 9, 2013 at 14:53
Marcos Gonzalez
1,0962 gold badges12 silver badges20 bronze badges
asked Mar 2, 2013 at 3:50
krizzokrizzo
1,8835 gold badges31 silver badges53 bronze badges
4 Answers
Reset to default 6Yes, there certainly is. There are (at least) four techniques you can use:
WebSockets. This is the most obvious solution. It allows you to send and receive messages on-demand without need for polling. It may, however, be somewhat more difficult to implement on the server side, as it is not plain HTTP. Also, old browsers don't support WebSockets.
Server-Sent Events. This is less desirable, but will still work for you. With it, you can receive messages from the server without polling. It is also easier to implement on the server as it's just plain HTTP except that the connection doesn't close. It, too, is not supported in some older web browsers, but it's more supported than WebSockets and is pretty easy to shim.
COMET. This is basically an improvement of the thing below. Basically, you have an
iframe
that's hidden offscreen. Whenever an event es in, you send (and flush!) a script tag, but don't close the connection. After a timeout, close the connection. Then refresh theiframe
. This is also pretty easy to implement, is plain HTTP, and needs no special browser support. However, browser timeouts vary and this is somewhat inelegant.Keeping the connection open until an event es. This is probably the least desirable. Simply don't send a response until an event es in. When an event es in or a timeout occurs, send the response. When the client receives the response, reconnect. This is also somewhat inelegant but it works.
What you are talking about is pushing from the server to the client which is et you can use such way
But this can be done perfectly with node.js and WebSockets in HTML5 but it's not supported in all browsers check this out
And you can make another thing
Send a request from your JS and make it alive for 1 minute and make an infinite loop that sleeps for some seconds and if it found anything while that return it to the client else make a new request this technique is called HeartBeat
I hope this can help
Simply, in most cases you will not be able to.. However, you can use HTML5 Websockets, though they are monly unsupported.
SockJS, uses a variety of methods to do callbacks in JavaScript, one of which is the blocking query, another WebSockets. I would highly remend it if you are trying to do this sort of thing.
You can do this using websockets. Unfortunately not all browsers currently support them.
More info on websockets here.
本文标签: javascriptAJAX listener rather than pollingStack Overflow
版权声明:本文标题:javascript - AJAX listener rather than polling? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403023a2468261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论