Permalink
Browse files

Use http-browserify

  • Loading branch information...
1 parent b8346c7 commit ce626592d60e866b7b64a1c688f5f5c36bc6fab3 @jinroh jinroh committed Jun 21, 2012
Showing with 17 additions and 25 deletions.
  1. +17 −25 lib/cube/emitter-http.js
@@ -1,5 +1,6 @@
-var util = require("util");
- // http = require("http");
+var util = require("util"),
+ // @browserify-alias --replace http-browserify
+ http = require("http");
module.exports = function(protocol, host, port) {
var emitter = {},
@@ -12,30 +13,21 @@ module.exports = function(protocol, host, port) {
var events = queue.splice(0, 500),
body = JSON.stringify(events);
- var xhr = new XMLHttpRequest();
- xhr.open("POST", "http://" + host + ":" + (port || 80) + "/1.0/event", true);
- xhr.setRequestHeader("Content-Type", "application/json");
- xhr.onreadystatechange = function(res) {
- if (res.status !== 200) return error(response.status);
+ http.request({
+ host: host,
+ port: port,
+ path: "/1.0/event",
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "Content-Length": body.length
+ }
+ }, function(response) {
+ if (response.statusCode !== 200) return error(response.statusCode);
if (queue.length) setTimeout(send, 500);
- }
- xhr.send(body);
-
- // http.request({
- // host: host,
- // port: port,
- // path: "/1.0/event",
- // method: "POST",
- // headers: {
- // "Content-Type": "application/json",
- // "Content-Length": body.length
- // }
- // }, function(response) {
- // if (response.statusCode !== 200) return error(response.statusCode);
- // if (queue.length) setTimeout(send, 500);
- // }).on("error", function(e) {
- // error(e.message);
- // }).end(body);
+ }).on("error", function(e) {
+ error(e.message);
+ }).end(body);
function error(message) {
util.log("error: " + message);

0 comments on commit ce62659

Please sign in to comment.