1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/xpcshell/node-http2/example/client.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,42 @@ 1.4 +var fs = require('fs'); 1.5 +var path = require('path'); 1.6 +var http2 = require('..'); 1.7 + 1.8 +http2.globalAgent = new http2.Agent({ 1.9 + log: require('../test/util').createLogger('client') 1.10 +}); 1.11 + 1.12 +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 1.13 + 1.14 +// Sending the request 1.15 +// It would be `var request = http2.get(process.argv.pop());` if we wouldn't care about plain mode 1.16 +var options = require('url').parse(process.argv.pop()); 1.17 +options.plain = Boolean(process.env.HTTP2_PLAIN); 1.18 +var request = http2.request(options); 1.19 +request.end(); 1.20 + 1.21 +// Receiving the response 1.22 +request.on('response', function(response) { 1.23 + response.pipe(process.stdout); 1.24 + response.on('end', finish); 1.25 +}); 1.26 + 1.27 +// Receiving push streams 1.28 +request.on('push', function(pushRequest) { 1.29 + var filename = path.join(__dirname, '/push-' + push_count); 1.30 + push_count += 1; 1.31 + console.error('Receiving pushed resource: ' + pushRequest.url + ' -> ' + filename); 1.32 + pushRequest.on('response', function(pushResponse) { 1.33 + pushResponse.pipe(fs.createWriteStream(filename)).on('finish', finish); 1.34 + }); 1.35 +}); 1.36 + 1.37 +// Quitting after both the response and the associated pushed resources have arrived 1.38 +var push_count = 0; 1.39 +var finished = 0; 1.40 +function finish() { 1.41 + finished += 1; 1.42 + if (finished === (1 + push_count)) { 1.43 + process.exit(); 1.44 + } 1.45 +}