Permalink
Browse files

Merge pull request #3 from sudodo/fix_json_parse

fix "Unexpected token ILLEGAL" error.
  • Loading branch information...
andregoncalves committed Sep 27, 2011
2 parents b92be75 + 05c1dae commit ed2353f73ecb8aca66294f0640b269297ebe1ddd
Showing with 18 additions and 5 deletions.
  1. +18 −5 server.js
View
@@ -25,16 +25,29 @@ var clients = [];
var twitter = http.createClient(80, "stream.twitter.com");
var request = twitter.request("GET", "/1/statuses/filter.json?track=" + KEYWORD, headers);
+
+var message = "";
+
request.addListener('response', function (response) {
response.setEncoding("utf8");
response.addListener("data", function (chunk) {
- // Send response to all connected clients
-
- clients.each(function(c) {
- c.write(chunk);
+
+ message += chunk;
+
+ var newlineIndex = message.indexOf('\r');
+ // response should not be sent until message includes '\r'.
+ // Look at the section titled "Parsing Responses" in Twitter's documentation.
+ if (newlineIndex !== -1) {
+ var tweet = message.slice(0, newlineIndex);
+ clients.forEach(function(client) {
+ // Send response to all connected clients
+ client.write(tweet);
+ });
+ }
+ message = message.slice(newlineIndex + 1);
});
- });
+
});
request.end();

0 comments on commit ed2353f

Please sign in to comment.