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
Add a ment  | 

4 Answers 4

Reset to default 6

Yes, there certainly is. There are (at least) four techniques you can use:

  1. 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.

  2. Server-Sent Events. This is less desirable, but will still work for you. With it, you can receive mes­sages 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 brow­sers, but it's more supported than WebSockets and is pretty easy to shim.

  3. 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 the iframe. This is also pretty easy to implement, is plain HTTP, and needs no special browser support. However, browser time­outs vary and this is somewhat inelegant.

  4. 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