Reputation: 3721
I'm trying to read in JSON objects over a websocket for visualisation by the D3 library. I'm just checking if the browser receives the JSON correctly. This is the code:
ws = new WebSocket("ws://localhost:8888/dh");
ws.onmessage = function(evt)
{
d3.json(
JSON.parse(evt.data),
function (jsondata) {
console.log(jsondata);
var data = jsondata.map(function(d) { return d.Value; });
console.log(data);
});
}
But when I check the console log it gives me these errors:
[19:15:36.434] TypeError: jsondata is null @ http://localhost:8888/static/anEx.js:21
and console.log(data) is null.
What is incorrect here?
Thanks
Upvotes: 0
Views: 350
Reputation: 165232
d3.json
is used to make HTTP requests that return JSON responses.
You already have the data in your WS message handler, there's no need to make another request. Just do:
ws.onmessage = function(evt) {
var jsondata = JSON.parse(evt.data);
console.log(jsondata);
// handle data ...
}
Upvotes: 1