Skip to content

Instantly share code, notes, and snippets.

@zxcabs
Created October 18, 2012 10:36
Show Gist options
  • Select an option

  • Save zxcabs/3910923 to your computer and use it in GitHub Desktop.

Select an option

Save zxcabs/3910923 to your computer and use it in GitHub Desktop.
test res.setEncoding('utf8')
var http = require('http');
function get() {
http.get("http://localhost:9345", function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('get chunk: ' + chunk);
data += chunk;
});
res.on('end', function () {
console.log(data);
});
}).on('error', function (err) {
console.log(err);
});
}
var server = http.createServer(function (req, res) {
var str = 'п1п1',
buff = new Buffer(str, 'utf8'),
l = buff.length;
(function send(i) {
var chunk;
if (l > i) {
chunk = buff.slice(i, i + 2);
console.log('send chunk: ' + chunk.toString('utf8'));
res.write(chunk);
setTimeout(function () {
i += 2;
send(i);
}, 300);
} else {
res.end();
server.close();
}
}(0));
});
server.listen(9345, get);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment