如何在dart中启动处理和读取stdin输入的参数



我想在dart中实现follow代码并启动V2ray

cat ~/.config/qv2ray/vcore/config.json | v2ray

这是Node.js实现:

const result = child_process.spawn("v2ray", [], { input: str });

我花了一整天的时间解决这个问题,但仍然无法解决

举了一个如何做到这一点的例子:

import 'dart:convert';
import 'dart:io';
Future<void> main() async {
final homeDir = getHomeDir();
if (homeDir == null) {
throw Exception('Could not find home directory on running platform!');
}
final process = await Process.start('v2ray', const []);
final resultStdoutFuture = process.stdout
.transform(const Utf8Decoder())
.transform(const LineSplitter())
.toList();
await process.stdin
.addStream(File('$homeDir/.config/qv2ray/vcore/config.json').openRead());
await process.stdin.close();
print('Process stopped with exit code: ${await process.exitCode}');
print('Returned stdout:');
(await resultStdoutFuture).forEach((logLine) => print('t$logLine'));
}
String? getHomeDir() {
final envVars = Platform.environment;
if (Platform.isMacOS) {
return envVars['HOME'];
} else if (Platform.isLinux) {
return envVars['HOME'];
} else if (Platform.isWindows) {
return envVars['UserProfile'];
}
}
import 'dart:io';
import 'dart:convert';
String server = '''
{
"field":"config"
}
''';
main() async {
var v2ray = await Process.start('v2ray', []);
v2ray.stdout.transform(utf8.decoder).forEach(print);
Stream.value(const Utf8Codec().encode(server)).pipe(v2ray.stdin);
}

最新更新