Created
January 13, 2013 22:19
-
-
Save thejhh/4526517 to your computer and use it in GitHub Desktop.
Test of a reverse HTTP request. Server will send data first, then wait for input from the client based on the data. Rude ugly code but it actually works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'); | |
| var options = { | |
| hostname: '192.168.56.101', | |
| port: 1337, | |
| path: '/', | |
| method: 'POST' | |
| }; | |
| var i = 0; | |
| var req = http.request(options, function(res) { | |
| console.log('STATUS: ' + res.statusCode); | |
| console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
| res.setEncoding('utf8'); | |
| res.on('data', function (chunk) { | |
| console.log('Received: "' + chunk + '"'); | |
| // Parse reply | |
| var data = JSON.parse(chunk); | |
| var obj = {}; | |
| Object.keys(data).forEach(function(key) { | |
| if( (typeof data[key] === 'string') && (data[key][0] === '#') ) { | |
| var id = parseInt( data[key].substr(10), 10); | |
| obj[key] = function() { | |
| req.write('#call#' + id + ";\n"); | |
| }; | |
| } else { | |
| obj[key] = data[key]; | |
| } | |
| }); | |
| // Do things in 250 ms | |
| setTimeout(function() { | |
| obj.inc(); | |
| }, 0.250*1000); | |
| }); | |
| res.on('end', function() { | |
| console.log("END of Response"); | |
| }); | |
| }); | |
| req.on('error', function(e) { | |
| console.error('ERROR: ' + e.message); | |
| }); | |
| req.write('#start;'+"\n"); | |
| // End test in 2 seconds | |
| setTimeout(function() { | |
| req.end(); | |
| }, 2*1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var data = { | |
| 'count':10, | |
| 'inc': function() { return this.count++; }, | |
| 'dec': function() { return this.count--; } | |
| }; | |
| var http = require('http'); | |
| http.createServer(function (req, res) { | |
| req.pause(); | |
| console.log('Started new request'); | |
| req.setEncoding('utf8'); | |
| var obj = {}, funs = []; | |
| Object.keys(data).map(function(key) { | |
| if(typeof data[key] === 'function') { | |
| funs.push(data[key].bind(data)); | |
| obj[key] = '#function[' + (funs.length-1) + ']'; | |
| } else { | |
| obj[key] = data[key]; | |
| } | |
| }); | |
| res.writeHead(200, {'Content-Type': 'text/plain'}); | |
| res.write(JSON.stringify(obj) + "\n"); | |
| req.on('data', function(chunk) { | |
| console.log("Received: '"+ chunk + "'"); | |
| var found = chunk.match(/#call#([0-9]+);/); | |
| if(found) { | |
| var id = parseInt(found[1], 10); | |
| console.log("Parsed #" + id + ", calling it next..."); | |
| if(funs[id]) { | |
| funs[id](); | |
| } else { | |
| console.error("No such function!"); | |
| } | |
| } | |
| }); | |
| req.on('end', function() { | |
| console.log("Received end event"); | |
| res.end(); | |
| }); | |
| req.resume(); | |
| }).listen(1337, '192.168.56.101'); | |
| console.log('Server running at http://192.168.56.101:1337/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment