简体   繁体   中英

jQuery setTimeout ajax recursive function throwing error after short while in instant messaging program

Like the title says, I'm writing an instant messaging program using jquery, ajax, mysql and php. Currently, I have a database with a table "switchboard" that contains the fields

mid*, from, to, message, timestamp, read

I have a function that uses setTimeout to recursively check every 1 to 3 seconds for new unread messages that is as follows:

function get_new_messages(){
    if( $('input[name="from"]:checked').val()
        && $('input[name="to"]:checked').val()
        && ready) {

        ready=false;
        //ajax call
        $.ajax({
            url: 'chatAjax.php',
            type: 'POST',
            data: {
                checker_from:$('input[name="from"]:checked').val(),
                check_to: $('input[name="to"]:checked').val()},
                success: function(data){
                    $('#chat_window').append(data);
                    ready=true;

            setTimeout(get_new_messages,1000);
                },
                error: function(){
                    ready=true; 
            setTimeout(get_new_messages,3000);
            }               
        }
        );
    } else{
        setTimeout(get_new_messages,3000)
    }
}

Right now I'm only testing, so checker_from and check_to correspond to radio buttons that contain a username. Using two browser windows I am attempting to send messages from one window to the other, however, after a while, the console shows consistent POST failures. Is this a memory leak, or something else. any help would be greatly appreciated

Please consider using Node.js and Socket.IO. It is much more efficient. My book goes over how to do this in full detail, but there are many Node/Chat demonstrations on the web. You could just use Node for the chat server and PHP for everything else.

The reason you are seeing POST errors is probably due to the constant polling that you are doing. Browsers generally have a limit on the number of concurrent connections to a given server. Traditionally, a domain was limited to 2 concurrent connections, although that is changed now in some browsers (I'm thinking Chrome). I suspect your are seeing errors when the POSTs start to "run over" each other and exceed the limit. Besides, polling like this will quickly kill a server in a mult-user system.

Instead of constantly polling, you want something that handles push messaging, like web sockets and long-polling fall-back. The Socket.IO and Strophie libraries provide this. I have experience with Strophie/XMPP - and I can confirm that it is a LOT more complicated. Socket.IO scales easily to thousands (or tens of thousands) per server. The solution you are currently pursuing is nowhere near as scalable :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM