admin管理员组文章数量:1321443
I'm starting on my first javascript GTK app and I want to download a file and track it's progress with a Gtk.ProgressBar. The only docs I can find about http requests are some example code here:
.js.html.en
And some confusing Soup reference here:
.2-gtk-3.0/gjs/Soup.SessionAsync.html
From what I can gather, I can do something like this:
const Soup = imports.gi.Soup;
var _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
var request = Soup.Message.new('GET', url);
_httpSession.queue_message(request, function(_httpSession, message) {
print('download is done');
}
There only seems to be a callback for when the download is done, and I can't find any way to set a callback function for any data events. How can I do this?
This is really easy in node.js:
var req = http.request(url, function(res){
console.log('download starting');
res.on('data', function(chunk) {
console.log('got a chunk of '+chunk.length+' bytes');
});
});
req.end();
I'm starting on my first javascript GTK app and I want to download a file and track it's progress with a Gtk.ProgressBar. The only docs I can find about http requests are some example code here:
http://developer.gnome/gnome-devel-demos/unstable/weatherGeonames.js.html.en
And some confusing Soup reference here:
http://www.roojs/seed/gir-1.2-gtk-3.0/gjs/Soup.SessionAsync.html
From what I can gather, I can do something like this:
const Soup = imports.gi.Soup;
var _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
var request = Soup.Message.new('GET', url);
_httpSession.queue_message(request, function(_httpSession, message) {
print('download is done');
}
There only seems to be a callback for when the download is done, and I can't find any way to set a callback function for any data events. How can I do this?
This is really easy in node.js:
var req = http.request(url, function(res){
console.log('download starting');
res.on('data', function(chunk) {
console.log('got a chunk of '+chunk.length+' bytes');
});
});
req.end();
Share
Improve this question
asked Feb 11, 2013 at 6:27
micahmicah
3532 silver badges10 bronze badges
1 Answer
Reset to default 9Thanks to help from the [email protected], I've figured it out. It turns out Soup.Message has events that you can bind to, including one called got_chunk and one called got_headers.
const Soup = imports.gi.Soup;
const Lang = imports.lang;
var _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
// variables for the progress bar
var total_size;
var bytes_so_far = 0;
// create an http message
var request = Soup.Message.new('GET', url);
// got_headers event
request.connect('got_headers', Lang.bind(this, function(message){
total_size = message.response_headers.get_content_length()
}));
// got_chunk event
request.connect('got_chunk', Lang.bind(this, function(message, chunk){
bytes_so_far += chunk.length;
if(total_size) {
let fraction = bytes_so_far / total_size;
let percent = Math.floor(fraction * 100);
print("Download "+percent+"% done ("+bytes_so_far+" / "+total_size+" bytes)");
}
}));
// queue the http request
_httpSession.queue_message(request, function(_httpSession, message) {
print('Download is done');
});
本文标签:
版权声明:本文标题:javascript - Using gjs, how can you make an async http request to download a file in chunks? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742100814a2420792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论