NodeJS合并对象

时间:2013-08-26 22:44:05

标签: javascript json node.js object

我正在研究NodeJS,以便创建一种将图片同时发送到多个显示器的方法,目前我的代码中包含以下语句:

var $   = require('jQuery');
var app = require('http').createServer(handler);
var io  = require('socket.io').listen(app, { log:false });
var fs  = require('fs');

var Server = {
    server_address : "XXX.XXX.XXX.XXX",  // The server address
    playing_now    : Object // The playlist that are being synced
};

var Client = {
    clients : Object
};

app.listen(1337);
console.log("App is running on port 1337...");

function handler(request, response) {
    var ip_address = null;

    try{ ip_address = request.header('x-forwarded-for'); }
    catch(error){ ip_address = request.connection.remoteAddress; }

    Client.clients[ip_address] = "X";

    fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) {
            response.writeHead(500);
            return response.end('Error loading index.html');
        }

        response.writeHead(200);
        response.end(data);
    });
}

io.sockets.on('connection', function (socket) {
    socket.on("RequestPlaylist", function(json){
        Client.clients[json["Address"]] = json["PlayList"];    

        if(typeof Server.playing_now[json["PlayList"]] === 'undefined'){
            $.ajax({
                url      : "http://" + Server.server_address + "/buildPlayList.php",
                type     : "GET",
                dataType : "jsonp",
                method   : "GET",
                data     : {RemoteIP : json["Address"], Name : json["PlayList"]}
            }).done(function(playlistJSON){
                console.log("________________________________________");
                console.log("Done");
                console.log(Server.playing_now);
                Server.playing_now[playlistJSON["playName"]] = playlistJSON;
                console.log(Server.playing_now);
                console.log("________________________________________");
            })    
        }else{
            console.log("________________________________________");
            console.log("Redirect");
            console.log(Server.playing_now);
            console.log("________________________________________");
        }    
    });
});

当新客户端连接时,我将它的IP地址存储在Client.clients上,当我得到包含图像URL的json时,我将它存储在Server.playing_now上。

问题是,当我输出Server.playing_now时,它包含了Client.clients的内容:

Done
{ [Function: Object] 'XXX.XXX.XXX.XXX': 'Default Media' }
{ [Function: Object]
  'XXX.XXX.XXX.XXX': 'Default Media',
  'Default Media':
   { '1':
      { json: [Object],
        duration: '5',
        name: 'Slideshow',
        type: 'Image',
        sync: '1',
        callback: 'jQuery17207063492417801172_1377555709748' },
     playName: 'Default Media' } }

如果它再次尝试获取数据,则结果为:

Redirect
{ [Function: Object]
  '105.102.15.234': 'Default Media',
  'Default Media':
   { '1':
      { json: [Object],
        duration: '5',
        name: 'Entrenamiento PL',
        type: 'Image',
        sync: '1',
        callback: 'jQuery17207063492417801172_1377555709748' },
     playName: 'Default Media' } }

我的代码中没有任何地方合并这两个对象,如果我注释掉Client对象,它只会返回Server.playing_now的内容。

知道会发生什么事吗?或者预期会出现这种情况吗?

1 个答案:

答案 0 :(得分:1)

初始化客户端(和服务器)时:

var Client = {
    clients : Object
};

您设置为Object这是同一件事(也是一个坏主意)。你真的想要new Object(){}

尝试将初始化代码更改为:

var Server = {
    server_address : "XXX.XXX.XXX.XXX",  // The server address
    playing_now    : {} // The playlist that are being synced
};

var Client = {
    clients : {}
};