dart-io

How do you run an interactive process in Dart?

♀尐吖头ヾ 提交于 2019-12-11 02:06:50
问题 The test below attempts to run the less pager command and return once the user quits. The problem is that it doesn't wait for user input, it just lists the entire file and exits. Platform: xubuntu 12.04, Dart Editor build: 13049. import 'dart:io'; void main() { shell('less', ['/etc/mime.types'], (exitCode) => exit(exitCode)); } void shell(String cmd, List<String> opts, void onExit(int exitCode)) { var p = Process.start(cmd, opts); p.stdout.pipe(stdout); // Process output to stdout. stdin.pipe

Dart HTTP server and Futures

↘锁芯ラ 提交于 2019-12-10 17:44:01
问题 I am trying to write simple HTTP server which parse result of client.getUrl(). I've got everything working except I am not able to write back to http request object (print to console works fine). Relevant code is: main() { HttpServer.bind(InternetAddress.ANY_IP_V4, 4040) .then((HttpServer server) { print('listening on localhost, port ${server.port}'); server.listen((HttpRequest request) { Future loadedContent = loadUrl(furl); loadedContent.then((content) => print(content)); // loadedContent

Monitor and restart Dart process on server

旧巷老猫 提交于 2019-12-09 23:17:59
问题 My lightweight dart:io based web server pretty much looks like this: import 'dart:io'; void main() { HttpServer.bind(InternetAddress.ANY_IP_V4, 80).then((server) { server.listen((HttpRequest request) { // ... do stuff... request.response.write('Alright, here is your response...'); request.response.close(); }); }); print("listing...."); } Let's launch it (on Ubuntu Server 1.04): $ nohup dart myServer.dart & Listening... Everything looking good so far. I can exit my shell and it keeps serving.

Doing an HTTP Post with headers and a body

蓝咒 提交于 2019-12-09 17:30:16
问题 Right, so I've been working on something which requires basic authentication through headers, and passing some variables via HTTP Post. This is a terminal app. This is what my code looks like: import 'package:http/http.dart' as http; import 'dart:io'; void main() { var url = "http://httpbin.org/post"; var client = new http.Client(); var request = new http.Request('POST', Uri.parse(url)); var body = {'content':'this is a test', 'email':'john@doe.com', 'number':'441276300056'}; request.headers

Dart: handle incoming HTTP requests in parallel

牧云@^-^@ 提交于 2019-12-09 14:25:50
问题 I am trying to write an HTTP server in Dart that can handle multiple requests in parallel. I have been unsuccessful at achieving the "parallel" part thus far. Here is what I tried at first: import 'dart:io'; main() { HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) { server.listen((HttpRequest request) { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); while (stopwatch.elapsedMilliseconds < 1000) { /* do nothing */ } request.response.statusCode = HttpStatus

Stream image file to HttpResponse efficiently

青春壹個敷衍的年華 提交于 2019-12-07 19:13:47
问题 My server-side Dart web app serves image files for certain requests. Simplified, here's what it currently does: HttpServer.bind(InternetAddress.ANY_IP_V4, 80) .then((HttpServer server) { server.listen((HttpRequest request) { request.response.statusCode = HttpStatus.OK; request.response.headers.contentType = ContentType.parse("image/jpg"); var file = new File("C:\\images\\myImage.jpg"); file.readAsBytes().then((List<int> bytes) { bytes.forEach((int b) => request.response.writeCharCode(b)); //

Stream image file to HttpResponse efficiently

ぃ、小莉子 提交于 2019-12-06 15:24:00
My server-side Dart web app serves image files for certain requests. Simplified, here's what it currently does: HttpServer.bind(InternetAddress.ANY_IP_V4, 80) .then((HttpServer server) { server.listen((HttpRequest request) { request.response.statusCode = HttpStatus.OK; request.response.headers.contentType = ContentType.parse("image/jpg"); var file = new File("C:\\images\\myImage.jpg"); file.readAsBytes().then((List<int> bytes) { bytes.forEach((int b) => request.response.writeCharCode(b)); // slow! request.response.close(); }); } } This works, but it's fairly slow and I suspect that writing

Clearing the terminal screen in a command-line Dart app

こ雲淡風輕ζ 提交于 2019-12-06 02:55:47
This one don't work (on Windows in a Cmd-Box): import 'dart:io'; void main() { print("Hello, World!"); Process.start('cls', [], runInShell: true).then((process) { stdout.addStream(process.stdout); stderr.addStream(process.stderr); }); } Günter Zöchbauer EDIT This seems to have the answer why it doesn't work on windows How to make win32 console recognize ANSI/VT100 escape sequences? ORIGINAL if(Platform.isWindows) { // not tested, I don't have Windows // may not to work because 'cls' is an internal command of the Windows shell // not an executeable print(Process.runSync("cls", [], runInShell:

Why can't Dart's “Process.start” execute an Ubuntu command when the command works in Ubuntu terminal?

折月煮酒 提交于 2019-12-05 17:45:46
I have command I would like to call with Dart. The command is sonar-runner which works perfectly if I run it in a normal Ubuntu terminal. This is because I have edited the PATH in the .profile file so it becomes a global command. However, if I wrote a simple Process.start code that should trigger the same thing: Process.run('sonar-runner', []).then((result) { stdout.write(result.stdout); stderr.write(result.stderr); }); I get as a response: Uncaught Error: ProcessException: No such file or directory Command: sonar-runner Unhandled exception: ProcessException: No such file or directory Command: