Skip to content

Instantly share code, notes, and snippets.

@compressed
Created August 13, 2013 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compressed/6225359 to your computer and use it in GitHub Desktop.
Save compressed/6225359 to your computer and use it in GitHub Desktop.
TradeKing json parsing example - messages may be sent in chunks
var message = ""
, first_data = true
// message terminator
// all JSON requests will terminate with }}
, terminator = "}}";
request = consumer.get(url, access_token, access_secret);
request.on("response", function (response) {
response.setEncoding("utf8");
response.on("data", function (data) {
// if first data, ignore it
// it's of the form {"status":"connected"}
if (first_data) {
first_data = false;
}
// otherwise continue parsing
else {
message += data;
}
var terminatorIndex = message.indexOf(terminator);
var didFindTerminator = terminatorIndex != -1;
if (didFindTerminator) {
var tick = message.slice(0, terminatorIndex + 2);
// handle tick in your code...
// ...
// store rest of message (if any)
message = message.slice(terminatorIndex + 3);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment