我开始使用我的第一个 javascript GTK 应用程序,我想下载一个文件并使用 Gtk.ProgressBar 跟踪它的进度。我能找到的关于 http 请求的唯一文档是这里的一些示例代码:
http://developer.gnome.org/gnome-devel-demos/unstable/weatherGeonames.js.html.en
还有一些令人困惑的汤参考:
http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Soup.SessionAsync.html
据我所知,我可以做这样的事情:
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');
}
下载完成时似乎只有一个回调,我找不到任何方法来为任何数据事件设置回调函数。我怎样才能做到这一点?
这在 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();