需要使用Socket io解析实时json文件

时间:2015-06-18 18:23:56

标签: json node.js parsing socket.io

首先是抬头!我对node.js和socket.io的世界很新 我有一个json文件,其中包含以下数据: -

{
"football": {

            "id": 1,
            "home": "Liverpool",
            "away": "Chelsea",
            "score": "1-0",
            "last scorer":"Gerrard"
             }
}

此文件会在几秒钟内实时更新。

我真正想要实现的是解析这个json并在客户端更新相同的html,除此之外我想监听json文件中的任何更改并再次更新到html客户端。我怎么能做到这一点,对不起,如果这个问题看起来很愚蠢,但任何建议都可以提供帮助。

2 个答案:

答案 0 :(得分:5)

我终于找到了一些东西并进行了一些调整并研究了其他答案,我终于制作了一个有效的代码 首先简要回顾一下这段代码的作用 看你的json文件(本例中是sports.json) 如果检测到更改,则只读取json文件(在本例中为sports.json) 然后将读取的json文件发送到连接的客户端

在客户端,只要您对json文件进行更改

,魔法就会开始

PS:有关fs.watch在手动编辑和保存时两次触发的讨论(我将很快提出解决方法并在此更新)

服务器端

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var jf = require('jsonfile'); //jsonfile module
var fs = require('fs'); //require file system

app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});



io.sockets.on('connection', function(socket) {
fs.watch("sports.json", function(event, fileName) { //watching my        sports.json file for any changes
    //NOTE: fs.watch returns event twice on detecting change due to reason that editors fire 2 events --- there are workarounds for this on stackoverflow

    jf.readFile('sports.json', function(err, data) { //if change detected read the sports.json 

        var data = data; //store in a var
        console.log('sent') //just for debugging
        socket.volatile.emit('notification', data); //emit to all clients
    });

});

});

http.listen(3000, function() { //listen to 3000
console.log('listening on *:3000');
});

客户端:

<!doctype html>
<html>
  <head>
    <title>Socket.IO data</title>
<body>


<p id ="data">A data will appear here on change</p>

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
 <script>
       var socket = io.connect('http://localhost:3000'); //you can replace localhost with your public domain name too!!

    socket.on('notification', function (data) {

        $('#data').text(data.football.home); //Liverpool

    });

    </script>
</body>

sports.json文件

{
"football": {

            "id": 1,
            "home": "Liverpool",
            "away": "Chelsea",
            "score": "1-0",
            "last scorer":"Gerrard"
             }
}

答案 1 :(得分:1)

(很抱歉,这个问题很简单,我有电脑问题但很难做很多事情;我们会在他们解决之后编辑它,一切都会停止崩溃)

要查找文件中的更改,请尝试以下操作:Monitor file change through AJAX, how?Check if file has changed using HTML5 File API

服务器文件:

var EventEmitter = require('events').EventEmitter;
var footballWatcher = new EventEmitter();

io.on('connection', function (client) {

//
//Existing Code
//

//Node.js Event listener:
footballWatcher.on('update', function(){
    io.emit('footballUpdate', footballData);
});

//Code to look for a change in the file, use this in it:
if(updated){
    footballWatcher.emit('update');        
});

客户档案:

//
//Existing Code (connections, methods, emits and responses, etc.)
//

socket.on('football', function(footballJSONData){
    //JSON interpretation
});