Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

revert http example

didnt mean to commit this, sorry for all the "misc"
commits while I was playing around :)
  • Loading branch information...
commit 2bb60d21de9e13e28ac43d8580e6891b89832046 1 parent c70bbbf
@tj tj authored
View
76 examples/http/app.js
@@ -1,76 +0,0 @@
-
-var zmq = require('../../')
- , sock = zmq.socket('dealer')
- , http = require('http')
- , Stream = require('stream')
-
-var server = http.createServer(function(req, res){
- // res.writeHead(200, req.headers);
- // req.on('data', function(chunk){
- // res.write(chunk);
- // }).on('end', function(){
- // res.end();
- // });
-
- console.log(req.method, req.url);
- var body = 'Hello';
- res.writeHead(200, { 'Content-Length': body.length });
- res.write(body);
- res.end();
-});
-
-var socks = {};
-
-sock.on('message', function(envelope, id, type, data){
- var id = id.toString()
- , type = type.toString()
- , req
- , res;
-
- console.log('%s %s', id, type);
-
- switch (type) {
- case 'request':
- req = new Stream;
- res = new Stream;
- res.socket = req;
-
- res.writeHead = function(status, headers){
- var obj = JSON.stringify({ status: status, header: headers });
- sock.send([envelope, id, 'response', obj]);
- };
-
- res.write = function(data){
- sock.send([envelope, id, 'data', data]);
- };
-
- res.end = function(){
- sock.send([envelope, id, 'end']);
- };
-
- socks[id] = req;
- data = JSON.parse(data.toString());
- req.url = data.url;
- req.method = data.method;
- req.headers = data.header;
- // console.log('%s : %s "%s"', id, req.method, req.url);
- server.emit('request', req, res);
- break;
- case 'data':
- // console.log();
- // console.log('data');
- // console.log(data.toString());
- req = socks[id];
- req.emit('data', data);
- break;
- case 'end':
- // console.log('end');
- req = socks[id];
- req.emit('end');
- delete socks[id];
- break;
- }
-});
-
-sock.connect('tcp://127.0.0.1:5001');
-console.log('app connected to :5001');
View
10 examples/http/broker.js
@@ -3,14 +3,12 @@ var zmq = require('../../')
, router = zmq.socket('router')
, dealer = zmq.socket('dealer');
-router.on('message', function(envelope, id){
- var args = Array.apply(null, arguments);
- dealer.send(args);
+router.on('message', function(){
+ dealer.send(Array.apply(arguments));
});
-dealer.on('message', function(envelope, id){
- var args = Array.apply(null, arguments);
- router.send(args);
+dealer.on('message', function(){
+ router.send(Array.apply(arguments));
});
router.bind('tcp://127.0.0.1:5000');
View
18 examples/http/node.js
@@ -1,18 +0,0 @@
-
-var http = require('http');
-
-var server = http.createServer(function(req, res){
- // res.writeHead(200, req.headers);
- // req.on('data', function(chunk){
- // res.write(chunk);
- // }).on('end', function(){
- // res.end();
- // });
-
- var body = 'Hello';
- res.writeHead(200, { 'Content-Length': body.length });
- res.write(body);
- res.end();
-});
-
-server.listen(3001);
View
56 examples/http/server.js
@@ -1,56 +0,0 @@
-
-var http = require('http')
- , zmq = require('../../')
- , sock = zmq.socket('dealer');
-
-var requests = {};
-
-http.createServer(function(req, res){
- var id = Date.now() + Math.random();
- // console.log('%s : %s "%s"', id, req.method, req.url);
-
- requests[id] = res;
-
- var obj = {
- method: req.method,
- url: req.url,
- header: req.headers
- };
-
- var json = JSON.stringify(obj);
-
- sock.send([id, 'request', json]);
-
- req.on('data', function(chunk){
- sock.send([id, 'data', chunk]);
- });
-
- req.on('end', function(){
- sock.send([id, 'end']);
- });
-}).listen(3000);
-
-sock.on('message', function(id, type, data){
- var id = id.toString()
- , type = type.toString()
- , res = requests[id];
-
- switch (type) {
- case 'response':
- data = JSON.parse(data);
- res.writeHead(data.status, data.header);
- break;
- case 'data':
- res.write(data);
- break;
- case 'end':
- res.end();
- delete requests[id];
- break;
- }
-});
-
-sock.connect('tcp://127.0.0.1:5000');
-
-console.log('HTTP server listening on :3000');
-console.log('dealer connected to :5000');
Please sign in to comment.
Something went wrong with that request. Please try again.