Skip to content
Browse files

Add http emitter

  • Loading branch information...
1 parent b8ec6c6 commit 543aa3275c610e787a3189fea6dba3c0d276b343 @trotter trotter committed May 8, 2012
Showing with 53 additions and 0 deletions.
  1. +51 −0 lib/cube/emitter-http.js
  2. +2 −0 lib/cube/emitter.js
View
51 lib/cube/emitter-http.js
@@ -0,0 +1,51 @@
+var util = require("util"),
+ http = require("http");
+
+module.exports = function(protocol, host, port) {
+ var emitter = {},
+ queue = [],
+ closing;
+
+ if (protocol != "http:") throw new Error("invalid HTTP protocol");
+
+ function send() {
+ var event = queue.pop();
+ if (!event) return;
+
+ var body = JSON.stringify(event);
+
+ var postOptions = {
+ host: host,
+ port: port,
+ path: "/1.0/event/put",
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Content-Length': body.length
+ }
+ };
+
+ var postRequest = http.request(postOptions, function() {});
+
+ postRequest.on('error', function (e) {
+ console.warn(e.message);
+ });
+
+ postRequest.write(body);
+ postRequest.end();
+
+ if (queue.length) process.nextTick(send);
+ }
+
+ emitter.send = function(event) {
+ if (!closing && queue.push(event) == 1) process.nextTick(send);
+ return emitter;
+ };
+
+ emitter.close = function () {
+ if (queue.length) closing = 1;
+ return emitter;
+ };
+
+ return emitter;
+};
View
2 lib/cube/emitter.js
@@ -1,5 +1,6 @@
var util = require("util"),
url = require("url"),
+ http = require("./emitter-http"),
udp = require("./emitter-udp"),
ws = require("./emitter-ws");
@@ -9,6 +10,7 @@ module.exports = function(u) {
switch (u.protocol) {
case "udp:": emitter = udp; break;
case "ws:": case "wss:": emitter = ws; break;
+ case "http:": emitter = http; break;
}
return emitter(u.protocol, u.hostname, u.port);
};

0 comments on commit 543aa32

Please sign in to comment.
Something went wrong with that request. Please try again.