使用Node.js和ajax实现长轮询

4
我有以下的服务器代码
var http = require('http');
var mysql = require('mysql');
var querystring = require('request');
var util = require('util');
var url = require('url');

var singer_name;
var currentmodif, lastmodif;
var requests=[];
var response;

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'someone',
  password : 'xxxxxxx',
  database : 'rest',  //mysql database to work with (optional)
});
connection.connect(); //connect to mysql

connection.query('SELECT * FROM musics WHERE id=1', function(err, rows, fields) {
  if (err) throw err;

  singer_name=rows[0].singer_name;
  currentmodif=rows[0].time_added;
});


http.createServer(function (req, res) {
    console.log('request received');

    requests.push({
        response: res,
        timestamp: new Date().getTime()
    });

    if(req.method=='GET'){
        var url_parts = url.parse(req.url,true);
        lastmodif = url_parts.query.timestamp;
    }

    //check_update(req, res);

}).listen(9000);


setInterval(function() {

    var expiration = new Date().getTime() - 30000;

    for (var i = requests.length - 1; i >= 0; i--) {
        //console.log("Request timestamp: "+requests[i].timestamp+" Expiration : "+expiration);
        response = requests[i].response;
        if (requests[i].timestamp < expiration) {
            console.log("The condition is met");
            response.writeHead(200, {
            'Content-Type'   : 'text/plain',
            'Access-Control-Allow-Origin' : '*'
            });

            // return response
            response.write('_testcb(\'ok\')', 'utf8');
            response.end();
            //break;
        }
    }

    connection.query('SELECT * FROM musics WHERE id=1', function(err, rows, fields) {
        if (err) throw err;
        currentmodif=rows[0].time_added;        
            //console.log("currentmodif: "+currentmodif+" lastmodif: "+lastmodif);
        if (currentmodif > lastmodif){
            singer_name=rows[0].singer_name; 
            var _arrays = {'singer_name': singer_name, 'time': currentmodif}
            var data = "_testcb"+"("+JSON.stringify(_arrays)+")";
            response.writeHead(200, {
            'Content-Type'   : 'text/plain',
            'Access-Control-Allow-Origin' : '*'
            });
            if (response.end(data))
            console.log("Response successfully sent");
            //return false;
        }

    });
}, 2000);

客户端代码:

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <title>Node.js Ajax test</title>
</head>
<body>

</body>
<script>
var timestamp = "1380020402";
function callNode() {

var time = "1380020402";
    $.ajax({
        url: 'http://xx.xxx.xx.xxx:9000/',
        dataType: "jsonp",
        data: {"timestamp":timestamp},
        type: 'POST',
        jsonpCallback: "_testcb",
        cache: false,
        timeout: 35000,
        success: function(response, code, xhr) {
            if ('ok' == response) {
                callNode();
                return false;
            }

            console.log(response);

            timestamp = response.time;
            // make new call
            callNode();
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log('error ' + textStatus + " " + errorThrown);
        }
    });
}
$(function () {
    callNode();
});
</script>
</html>

我正在尝试进行长轮询。因此,在数据库中的数据更新之前,应该暂停对Ajax请求的响应,但上述代码无效。我从不同的域发出Ajax请求,因此使用jsonp。
确切的问题是,当数据库中的数据更改时,响应不会被发送。它有时可以工作,但可靠性不一致。
另一个问题是超时代码块无法正常工作。如果请求已经30秒了,则应发送空白响应以避免Ajax超时。
如果有人能帮忙,我会非常感激。
干杯。
1个回答

6
我已经想出来了。下面是可行的修正代码:
客户端代码:
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <title>Node.js Ajax test</title>
</head>
<body>

</body>
<script>
var timestamp;
function callNode() {

    $.ajax({
        url: 'http://xx.xxx.xx.xxx:9000/',
        dataType: "jsonp",
        data: {"timestamp":timestamp},
        //type: 'POST', //don't need this with jsonp
        jsonpCallback: "_testcb",
        cache: false,
        timeout: 35000,
        success: function(response, code, xhr) {
            if ('ok' == response) {
                console.log(response);
                callNode();
                return false;
            }

            console.log(response);

            timestamp = response.time;
            // make new call
            callNode();
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log('error ' + textStatus + " " + errorThrown);
        }
    });
}
$(function () {
    setTimeout(callNode, 1); //call function with setTimeout in order to avoid ugly constant browser loading 
});
</script>
</html>

服务器端(server.js):
var http = require('http');
var mysql = require('mysql');
var util = require('util');
var url = require('url');

var singer_name, currentmodif, lastmodif, request, response, time_of_request;

//var requests=[];

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'someone',
  password : 'xxxxxx',
  database : 'rest',  //mysql database to work with (optional)
});
connection.connect(); //connect to mysql

connection.query('SELECT * FROM musics WHERE id=1', function(err, rows, fields) {
  if (err) throw err;

  singer_name=rows[0].singer_name;
  currentmodif=rows[0].time_added;
});


http.createServer(function (req, res) {
    request = req;
    response = res;
    time_of_request = new Date().getTime();
    console.log('request received');


    if(req.method=='GET'){
        var url_parts = url.parse(req.url,true);
        lastmodif = url_parts.query.timestamp;
    }

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    //checkupdate();     

}).listen(9000);

var response;

function checkupdate() { 

    var expiration = new Date().getTime() - 30000;

    //for (var i = requests.length - 1; i >= 0; i--) {
        //console.log("Request timestamp: "+time_of_request+" Expiration : "+expiration);
        if (time_of_request < expiration) {
            console.log("The condition is met");
            // return response
            response.write('_testcb(\'ok\')', 'utf8');
            response.end();
        }
    //}

    connection.query('SELECT * FROM musics WHERE id=1', function(err, rows, fields) {
        if (err) throw err;
        currentmodif=rows[0].time_added;

        if (lastmodif == undefined)
            lastmodif = 0;

        console.log("currentmodif: "+currentmodif+" lastmodif: "+lastmodif);

        if (currentmodif > lastmodif){
            singer_name=rows[0].singer_name;  
            var _arrays = {'singer_name': singer_name, 'time': currentmodif} 
            var data = "_testcb"+"("+JSON.stringify(_arrays)+")";

            //response.writeHead(200, { 'content-type':'application/json',
                                    //'Access-Control-Allow-Origin' : '*'});
            //response.write(data); 
            response.end(data);
            console.log("Response successfully sent");
            //return false;
        }

    });
};

setInterval(checkupdate, 2000);

问题在服务器端。当服务器想要回复时,响应对象不可用(未定义),因此响应未被发送。我可能在node.js的控制台中忽略了这个错误。
这几乎是一个完整的long polling示例,使用node.js和MYSQL数据库。该脚本将等待新数据可用后才回复ajax请求。如果从MYSQL中没有在请求后的30秒内获得新数据,则会进行虚假回复,以使请求不超时。ajax成功回调中有一个条件,在收到此演示响应时重新启动此ajax请求,从而使其成为无限循环。
我已经成功测试了上面的代码,并且似乎工作正常。我运行了脚本,然后更新了我的数据库中的数据(主要是time_added字段),这触发了来自node.js服务器的等待ajax调用的新数据的回复。
我希望这段代码能够帮助某些人。
查看此处的教程以获取进一步的解释:http://www.sahilsaid.com/blog/long-polling-node-js-mysql-database-ajax/

1
嗨。函数的递归调用会一直导致堆栈溢出吗? - Bin Wang
我不认为会出现这种情况。我的服务器已经持续运行了一个多月,但还没有导致任何溢出问题。也许node.js有应对这个问题的机制? - Sahil
1
这不会导致堆栈溢出。实际上,这并不是递归,因为callNode是由$.ajax请求的success回调函数调用的,而不是callNode调用自身。一旦callNode发起请求,它就会退出。 - Ivy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接