Permalink
Browse files

support for multiple clients

  • Loading branch information...
1 parent f5448a7 commit 6aebcc610e9b926d437207a91c40d34d1781c345 @andregoncalves committed Dec 30, 2009
Showing with 39 additions and 21 deletions.
  1. +1 −0 .gitignore
  2. +28 −19 server.js
  3. +8 −0 vendor/arrays.js
  4. +2 −2 vendor/ws.js
View
@@ -0,0 +1 @@
+*~
View
@@ -1,39 +1,48 @@
var sys = require('sys'),
http = require('http'),
- ws = require("./vendor/ws"), // Mininal websockets library
- base64 = require('./vendor/base64'); // Base64 encoding library
-
-var USERNAME = '';
-var PASSWORD = '';
-
+ ws = require("./vendor/ws"),
+ base64 = require('./vendor/base64'),
+ arrays = require('./vendor/arrays');
+
+// Command line args
+var USERNAME = process.ARGV[2];
+var PASSWORD = process.ARGV[3];
+var KEYWORD = process.ARGV[4] || "iphone";
+
+if (!USERNAME || !PASSWORD)
+ return sys.puts("Usage: node server.js <twitter_username> <twitter_password> <keyword>");
+
+// Authentication Headers for Twitter
var headers = [];
var auth = base64.encode(USERNAME + ':' + PASSWORD);
headers['Authorization'] = "Basic " + auth;
headers['Host'] = "stream.twitter.com";
-//headers['Transfer-Encoding'] = "chunked";
-ws.createServer(function (websocket) {
-
- // Connect to Twitter's streaming API
- var twitter = http.createClient(80, "stream.twitter.com");
- var request = twitter.request("GET", "/1/statuses/filter.json?track=iphone", headers);
+var clients = [];
- request.finish(function (response) {
- //sys.puts("STATUS: " + response.statusCode);
- //sys.puts("HEADERS: " + JSON.stringify(response.headers));
- response.setBodyEncoding("utf8");
- response.addListener("body", function (chunk) {
- // Send response to websocket clients
- websocket.send(chunk);
+// Connection to Twitter's streaming API
+var twitter = http.createClient(80, "stream.twitter.com");
+var request = twitter.request("GET", "/1/statuses/filter.json?track=" + KEYWORD, headers);
+request.finish(function (response) {
+ response.setBodyEncoding("utf8");
+ response.addListener("body", function (chunk) {
+ // Send response to all connected clients
+ clients.each(function(c) {
+ c.send(chunk);
});
});
+});
+// Websocket TCP server
+ws.createServer(function (websocket) {
+ clients.push(websocket);
websocket.addListener("connect", function (resource) {
// emitted after handshake
sys.debug("connect: " + resource);
}).addListener("close", function () {
// emitted when server or client closes connection
+ clients.remove(websocket);
sys.debug("close");
});
}).listen(8080);
View
@@ -0,0 +1,8 @@
+Array.prototype.remove = function(e) {
+ for (var i = 0; i < this.length; i++)
+ if (e == this[i]) return this.splice(i, 1);
+}
+
+Array.prototype.each = function(fn) {
+ for (var i = 0; i < this.length; i++) fn(this[i]);
+}
View
@@ -69,7 +69,7 @@ exports.createServer = function (websocketListener) {
}
}
- socket.send(nano(handshakeTemplate, {
+ socket.send(nano(handshakeTemplate, {
resource: matches[0],
host: matches[1],
origin: matches[2],
@@ -78,7 +78,7 @@ exports.createServer = function (websocketListener) {
handshaked = true;
emitter.emit("connect", matches[0]);
}
-
+
socket.addListener("receive", function (data) {
if(handshaked) {
handle(data);

0 comments on commit 6aebcc6

Please sign in to comment.