如何启动进程并从 dart 中的 stdin 输入读取参数?
How start process and read paramters from stdin input in dart?
我想在 dart 中实现以下代码并开始 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);
}
我想在 dart 中实现以下代码并开始 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);
}