Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Add http emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
trotter committed May 9, 2012
1 parent b8ec6c6 commit 543aa32
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 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;
};
2 changes: 2 additions & 0 deletions 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");

Expand All @@ -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.