S
S
SuperKozel2012-04-20 13:27:08
Node.js
SuperKozel, 2012-04-20 13:27:08

Long polling with node.js(express)?

I'm trying to implement long polling in node js. It works, but it's crooked. The problem is that most of the polling requests do not receive a response from the server. Already scratched my head why it doesn't work.
The idea is this:
Listeners are hung on game events, a res-object is bound to the listener's callback.
The browser keeps the request open until it receives a response, the game must keep listeners with the Res of these requests until it receives an event signal and the callback is fired.
But such a thing happens consistently: the Ajax request hangs in the browser, the event has passed, but the browser has not received a response.
game - an instance of the backbone model
I would be very grateful for the help.

app.all('/game/:game_id/update', function(req, res){
    var gameId = req.param('game_id');
    var game = games.get(gameId);
    if (!game){
        res.send('No game', 404);
    }

    var client = res;
    game.on('game:endTurn game:start game:startTurn', function(){
        this.json({update: true});
        game.off(null, null, this);
    }, client);

//    //через 60 секунд сбросить
//    setTimeout(function(){
//        game.off(null, null, this);
//    }, 60000)
});

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SuperKozel, 2012-04-20
@SuperKozel

thanks for the replies, looks like it was jquery caching

    function gamePolling(){
        $.ajax({
            url: "/game/<%=game.id%>/update",
            dataType: 'json',
            cache: false,
            success: function(data){
                if (data.update == true){
                    window.location.reload();
                }
                else{
                    gamePolling();
                }
            },
            error: function(data, type){
                gamePolling();
            }
        });
    }

N
nill, 2012-04-20
@nill

Why don't you try socket.io ?

A
Anatoly, 2012-04-20
@taliban

This, as far as I understand, is the client code. Maybe the problem is in the server side? Maybe he doesn't send anything at all?

V
Vitaly Zheltyakov, 2012-04-20
@VitaZheltyakov

Check the behavior of sessions, maybe this is it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question