Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active November 12, 2022 16:35
Show Gist options
  • Save ammarfaizi2/56d7fed30ea7120fa8f40b1e5532e0bc to your computer and use it in GitHub Desktop.
Save ammarfaizi2/56d7fed30ea7120fa8f40b1e5532e0bc to your computer and use it in GitHub Desktop.
const { CNRingMaster } = require('../../chnet/chnet.js');
function do_http_get() {
let ring = new CNRingMaster;
let ch = ring.CreateNet("https://www.google.com");
/*
* This user data can be set to anything. It will
* be passed into the callback and accessible via
* ch.udata variable.
*/
ch.SetUserData({
"test": "abc123",
"req_id": 123123,
"buf": ""
});
ch.SetRequestHeaders({
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
});
ch.On("headers_received", function (ch, hdr) {
console.log(hdr);
});
/*
* ch.PrepRead() at the bottom of this function
* performs asynchronous read. This read_completed
* callback will be invoked when the read completes.
*/
ch.On("read_completed", function (ch, res) {
ch.udata.buf += ch.chnet.read_buf();
/*
* Number of bytes successfully read.
*/
// console.log(res);
/*
* Do the read again.
*/
ch.PrepRead(1024);
});
/*
* When all body bytes has been read, this eof callback
* will be invoked. The HTTP request ends here.
*/
ch.On("eof", function (ch, res) {
console.log(ch.udata.buf);
ch.chnet.Close();
});
/*
* Something went wrong.
*/
ch.On("error", function (ch, code, err) {
console.log(ch.udata.buf);
console.log(code);
console.log(`Error: ${err}`);
ch.chnet.Close();
});
/*
* Read 1024 bytes.
*/
ch.PrepRead(1024);
}
do_http_get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment