I would like to know how can I get the JSON objects by position. I'm building an Websocket that will send to the client certain values from time to time, I did this code using Ajax, and I want to follow the same logic:
$.ajax({
type: 'GET',
url: 'locals.json',
data: {
get_param: 'locals'
},
dataType: 'json',
success: function(data) {
$.each(data.locals, function(index, locals) {
var count = 0;
setInterval(function() {
count++;
if (index == count) {
console.log(locals.lat);
}
}, 5000);
});
}
});
This code shows me every 5 seconds a different element from my JSON Object, and I would like to follow the same logic on Node.js, but I don't know how to get the index.
I'm using node.js to build my server.
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
var fs = require('fs');
var jsonObj;
fs.readFile('locations.json', 'utf8', function(err, data) {
if (err) throw err;
jsonObj = JSON.parse(data);
for (var key in jsonObj) {
}
});
} else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
How can I adapt my first code to the second one?
Thank you.
via RĂºben Dias
No comments:
Post a Comment