dart-io

Monitor and restart Dart process on server

六眼飞鱼酱① 提交于 2019-12-04 20:37:01
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. However, if something goes terribly wrong - e.g. an unhandled exception is thrown - the Dart process

Reading static files under a library in Dart?

可紊 提交于 2019-12-04 16:57:07
I am writing a library in Dart and I have static files under the library folder. I want to be able to read those files, but I'm not sure how to retrieve the path to it... there is not __FILE__ or $0 like in some other languages. Update: It seems that I was not clear enough. Let this help you understand me: test.dart import 'foo.dart'; void main() { print(Foo.getMyPath()); } foo.dart library asd; class Foo { static Path getMyPath() => new Path('resources/'); } It gives me the wrong folder location. It gives me the path to test.dart + resources/ , but I want the path to foo.dart + resources/ .

How can DELETE requests be made with a Dart HttpClient?

戏子无情 提交于 2019-12-04 05:25:42
问题 If you're using a Dart HttpClient (which provides an HttpClientRequest ) to make requests from a server to another server, as far as I can tell the only HTTP methods available are GET and POST (corresponding, respectively, to the post / postUrl and get / getUrl functions). Is there also a way to make PUT and DELETE requests? 回答1: You should be able to do with this with the open method which allows you to use any HTTP verb: client.open('delete', 'http://example.com', '8080', '/test'); If you

Doing an HTTP Post with headers and a body

风流意气都作罢 提交于 2019-12-04 05:11:47
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[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8'; request.headers[HttpHeaders

Dart: handle incoming HTTP requests in parallel

筅森魡賤 提交于 2019-12-04 01:35:21
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.OK; request.response.write(stopwatch.elapsedMilliseconds.toString()); request.response.close()

How do I list the contents of a directory with Dart?

一世执手 提交于 2019-12-03 23:30:58
I would like to list all the contents of a directory (on the file system) using Dart. How can I do this? Nico The API has changed and I have updated the async code for M4 release (0.5.16_r23799 ): Future<List<FileSystemEntity>> dirContents(Directory dir) { var files = <FileSystemEntity>[]; var completer = new Completer(); var lister = dir.list(recursive: false); lister.listen ( (file) => files.add(file), // should also register onError onDone: () => completer.complete(files) ); return completer.future; } This answer is out of date. Please see the accepted answer. There are two ways to list the

How can DELETE requests be made with a Dart HttpClient?

◇◆丶佛笑我妖孽 提交于 2019-12-02 04:04:28
If you're using a Dart HttpClient (which provides an HttpClientRequest ) to make requests from a server to another server, as far as I can tell the only HTTP methods available are GET and POST (corresponding, respectively, to the post / postUrl and get / getUrl functions). Is there also a way to make PUT and DELETE requests? You should be able to do with this with the open method which allows you to use any HTTP verb: client.open('delete', 'http://example.com', '8080', '/test'); If you look at the HttpClient source you'll see that the get and post methods are just aliases for open anyway:

How to set the current working directory in Dart? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-01 20:07:41
This question already has an answer here: How do I find the current directory, in Dart? 1 answer In a dart console application, how do I set the current working directory? import 'dart:io'; ... Directory.current = new Directory('your/path/here'); or just Directory.current = 'your/path/here'; See also https://api.dartlang.org/133671/dart-io/Directory/current.html 来源: https://stackoverflow.com/questions/33020117/how-to-set-the-current-working-directory-in-dart

How to catch SIGINT for the current in Dart?

℡╲_俬逩灬. 提交于 2019-12-01 06:15:33
How can Ctrl + C or SIGINT be caught in a Dart program for the current process? Something similar to this for Node: process.on('SIGINT', function() { // do stuff }); I found the following test code at Unified Diff: tests/standalone/io/signals_test_script.dart import "dart:io"; void main(args) { int usr1Count = int.parse(args[0]); int usr2Count = int.parse(args[1]); var sub1; var sub2; void check() { if (usr1Count < 0 || usr2Count < 0) exit(1); if (usr1Count == 0 && usr2Count == 0) { sub1.cancel(); sub2.cancel(); } print("ready"); } sub1 = ProcessSignal.SIGUSR1.watch().listen((signal) { if