aria2 adduri

demo, ok

 

 

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
import 'package:stream_channel/stream_channel.dart';
import 'dart:io';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/io.dart';
import 'dart:convert';

void main() => runApp(MaterialApp(
  home: MyApp(),
));


class MyApp extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  var permit;
  Directory setupPath;
  Directory sdCard;
  File aria2Main;
  IOWebSocketChannel channel;

  checkPermission()async{
    permit = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
    if(permit!=PermissionStatus.granted){
      await PermissionHandler().requestPermissions([PermissionGroup.storage]);
    }
  }

  setupFile()async{
    setupPath = await getApplicationDocumentsDirectory();
    aria2Main = File(join(setupPath.path, 'aria2'));
    bool existFile = await aria2Main.exists();
    if(!existFile) {
      print('no exist so to create it');
    var aria2File = await rootBundle.load('assets/aria2c');
    var aria2FileBuf = aria2File.buffer;
    aria2Main.writeAsBytesSync(aria2FileBuf.asUint8List());

    await Process.run('chmod', ['755', aria2Main.path]).then((e) {
      print(e.stdout);
      print(e.stderr);
      print(e.exitCode);
    });
    }

    var configFile = await rootBundle.load('assets/aria2.conf');
    var confbuf = configFile.buffer;
    File(join(setupPath.path, 'aria2.conf')).writeAsBytesSync(confbuf.asUint8List());

    print('start servicess rpc:');
    await Process.run(aria2Main.path, ['--conf-path=${setupPath.path}//aria2.conf']).then((e){
      print(e.stdout);
      print(e.stderr);
      print(e.exitCode);
    });
  }

  startService()async{
    channel = IOWebSocketChannel.connect('ws://localhost:6800/jsonrpc');
    channel.stream.listen((data){
      print('data: $data');
    });
  }

  downloadFile()async{
    sdCard = await getExternalStorageDirectory();
    String url = 'http://speed.189.cn/upload/BSSAClientSetup9.exe';
    Map xx = {"jsonrpc":"2.0","id":"m","method":"aria2.addUri","params":[["$url"]]};
    channel.sink.add(json.encode(xx));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Aria2'),),
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            RaisedButton(child: Text('checkPermission'),onPressed: (){
              checkPermission();
            },),
            RaisedButton(child: Text('setupFile'),onPressed: (){
              setupFile();
            },),
            RaisedButton(child: Text('start service'),onPressed: (){
              startService();
            },),
            RaisedButton(child: Text('Download'),onPressed: (){
              downloadFile();
            },),
          ],
        ),
      ),
    );
  }
}

  

 

V/BoostFramework(13547): BoostFramework() : mPerf = com.qualcomm.qti.Performance@667a10e
I/flutter (13547): start servicess rpc:
I/flutter (13547): data: {"id":"m","jsonrpc":"2.0","result":"3a85b5cc8bda8388"}
I/flutter (13547): data: {"jsonrpc":"2.0","method":"aria2.onDownloadStart","params":[{"gid":"3a85b5cc8bda8388"}]}
I/flutter (13547): data: {"jsonrpc":"2.0","method":"aria2.onDownloadComplete","params":[{"gid":"3a85b5cc8bda8388"}]}

  

    原文作者:CrossPython
    原文地址: https://www.cnblogs.com/pythonClub/p/10908386.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞