Using gjs, how can you make an async http request to download a file in chunks?
Asked Answered
L

1

11

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.org/gnome-devel-demos/unstable/weatherGeonames.js.html.en

And some confusing Soup reference here:

http://www.roojs.org/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();
Liguria answered 11/2, 2013 at 6:27 Comment(0)
L
9

Thanks 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');
});
Liguria answered 11/2, 2013 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.