Created
August 13, 2013 20:29
-
-
Save compressed/6225359 to your computer and use it in GitHub Desktop.
TradeKing json parsing example - messages may be sent in chunks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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