본문 바로가기

Programming!

longpolling 소켓의 대안..

setInterval의 경우 잘못하면 수십 수백개의 쓰래드가 생길 가능성이 있다.

(function poll(){
        $.ajax(
            {
            type:'get',
            url: "http://서버/new?callback=",
            success: function(data){
                $('#messageText').val(data);
            },
            complete: poll,
            timeout: 30000,
            async: true,
            cache: false,
        });
    })();

이렇게 complete 될경우 재귀호출하는 방식으로 처리하면 하나의 요청이 계속 유지된다.

http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery