Skip to content

Commit

Permalink
Merge pull request #3 from sudodo/fix_json_parse
Browse files Browse the repository at this point in the history
fix "Unexpected token ILLEGAL" error.
  • Loading branch information
andregoncalves committed Sep 27, 2011
2 parents b92be75 + 05c1dae commit ed2353f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions server.js
Expand Up @@ -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();

Expand Down

0 comments on commit ed2353f

Please sign in to comment.