-1

我每秒钟都在处理来自 URL 的请求数据

exports.getFootballNotRunning = function (callback) {

request({
    method: 'GET',
    url: 'http://ufxyz.ufabet.com/_View/RMOdds1Gen.ashx?ot=t&sort=0&at=EU',
}, function (error, response, body) {
    body = body.replace(/'/g, '"');
    body = JSON.parse(body);
    var setoffootball = [];
    for (var i = 0; i < body[2].length; i++) {
        setoffootball[i] = {
            league: body[2][i][0][1],
            matches: []
        };
        for (var j = 0; j < body[2][i][1].length; j++) {
            setoffootball[i].matches.push({
                firstteam: body[2][i][1][j][21],
                secondteam: body[2][i][1][j][23],
                time: body[2][i][1][j][10],
                fulltime: {
                    hdp: body[2][i][1][j][19],
                    h: body[2][i][1][j][32],
                    a: body[2][i][1][j][33],
                    goal: body[2][i][1][j][38],
                    over: body[2][i][1][j][39],
                    under: body[2][i][1][j][40]
                },
                firsthalf: {
                    hdp: body[2][i][1][j][49],
                    h: body[2][i][1][j][53],
                    a: body[2][i][1][j][54],
                    goal: body[2][i][1][j][57],
                    over: body[2][i][1][j][58],
                    under: body[2][i][1][j][59]
                }
            });
        }

    }
    notrunningfootball = setoffootball;
    callback(setoffootball);

});

}

就像这样,每秒钟我都想检查我收到的 Array JSON 哪个 json 与前一个不同,然后我可以发送通过 socket.io 更改的 JSON 并在客户端表中更新

var io = require('socket.io')();
io.on('connection', function (socket) {
    console.log('test');
    sendMatches();
});
io.listen(7000);

function sendMatches() {
    setTimeout(function () {
        FT.getAllMatches(function (run, notrun) {
            io.emit('running', run);
            io.emit('notrunning', notrun);
            sendMatches();
        })
    }, 1000);
}

JSON 示例,它是一个包含许多 json 对象的数组,示例只是一个 json

[{"league":"ENGLISH PREMIER LEAGUE","matches":[{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.25,"h":-9.7,"a":9.3,"goal":2,"over":8.6,"under":-9.3},"firsthalf":{"hdp":0,"h":6.9,"a":-7.8,"goal":0.75,"over":8.5,"under":-9.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0,"h":6.7,"a":-7.1,"goal":2.25,"over":-8.4,"under":7.7},"firsthalf":{"hdp":0.25,"h":-6.8,"a":5.9,"goal":1,"over":-7.3,"under":6.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.5,"h":-7.2,"a":6.8,"goal":1.75,"over":6.5,"under":-7.2},"firsthalf":{"hdp":0,"h":0,"a":0,"goal":0,"over":0,"under":0}}]}]

流程是这样的运行 app.js -> 服务器端:请求数据 -> 服务器端:将整个数据发送到客户端 -> 客户端:将所有数据提取到表中 -> 下 1 秒 -> 服务器端:请求数据 -> 服务器side : 比较旧数据和新数据,找到变化的 json -> 服务端:将与上一个不同的 json 发送给客户端 -> 客户端:更新新的 json 到 table

4

1 回答 1

0

我不认为我说对了..如果您只是想检查是否有区别,您可以简单地比较刺痛吗?...

JSON.stringify(a) === JSON.stringify(b)

这会告诉你是否有变化。要找到差异,您可以进行字符串操作或使用下划线 Lib (_) 来帮助您。

var o1 = {a: 1, b: 2, c: 2},
    o2 = {a: 2, b: 1, c: 2};

_.omit(o1, function(v,k) { return o2[k] === v; })

结果在 o1 中对应但在 o2 中具有不同值的部分:

{a: 1, b: 2}

深度差异会有所不同:

function diff(a,b) {
    var r = {};
    _.each(a, function(v,k) {
        if(b[k] === v) return;
        r[k] = _.isObject(v)
                ? _.diff(v, b[k])
                : v
            ;
        });
    return r;
}
于 2017-11-21T06:54:13.673 回答