Files
PixelStreamingSystem/source/lib/http_client.js
T
2022-12-29 16:16:19 +05:00

81 lines
2.2 KiB
JavaScript

const http = require('http')
const https = require('https')
module.exports = class http_client {
constructor(ip, port) {
this.#ip = ip
this.#port = port
}
async post(data, callback_answer, callback_error) {
// Build the post string from an object
// An object of options to indicate where to post to
var post_options = {
host: this.#ip,
port: this.#port,
method: 'POST',
headers: {
'Content-Type': 'applicaiton/json',
'Content-Length': Buffer.byteLength(data)
}
}
// Set up the request
var request = http.request(post_options, function(res) {
res.setEncoding('utf8')
res.on('data', async function (answer) {
callback_answer(answer)
})
})
request.on('error', function(e) {
callback_error(e)
})
// post the data
request.write(data)
request.end()
}
#ip
#port
}
module.exports.post = async (options, callback_res, callback_err) => {
http.post(options, function(res) {
var body_chunks = []
res.on('data', chunk => {
body_chunks.push(chunk)
}).on('end', () => {
var body = Buffer.concat(body_chunks)
callback_res(body)
})
}).on('error', err => {
callback_err(err)
})
}
module.exports.get = async (options, callback_res, callback_err) => {
http.get(options, function(res) {
var body_chunks = []
res.on('data', chunk => {
body_chunks.push(chunk)
}).on('end', () => {
var body = Buffer.concat(body_chunks)
callback_res(body)
})
}).on('error', err => {
callback_err(err)
})
}
module.exports.gets = async (options, callback_res, callback_err) => {
https.get(options, function(res) {
var body_chunks = []
res.on('data', chunk => {
body_chunks.push(chunk)
}).on('end', () => {
var body = Buffer.concat(body_chunks)
callback_res(body)
})
}).on('error', err => {
callback_err(err)
})
}