Heres my client-side code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Alerter</title>
</head>
<body>
<script src="jquery.min.js"></script>
<script src="http://www.example.com:8001/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
var socket = io.connect('http://www.example.com:8001');
socket.on('message', function (data) { do_message(data) });
});
function do_message(data){
var $obj=$(jQuery.parseJSON(data));
//now what goes in here?
};
</script>
</body>
</html>
And the javascript object I receive looks like
{
"root": {
"status": [
"OK"
],
"alert": [
{
"$": { "src": "web" },
"time": [ "1349316382" ],
"id": [ "user1" ]
},
{
"$": { "src": "web" },
"time": [ "1349316391" ],
"id": [ "user2" ]
},
]
}
}
And what I want to do is iterate over each of the “alert” entries and take appropriate action (the actual schema is a bit more complicated than I’ve shown above)
Are jQuery selectors the right tool for this task or should I got back to native javascript (ie getElementsByTagName or similar). All the jQuery docs are based around selecting valid html tags from the DOM, I cant figure out how to select arbitary tags from the received message.
You can iterate over the data using
$.forEach:Or you can just use regular JS: