私はダーツのクライアント/サーバーでいくつかの素晴らしいチュートリアルを見つけました。クライアントは指定されたポートのlocalhostを介してサーバーに要求を行い、サーバーはStringで応答します。ダーツのクライアントサーバー
しかし、私は画像を提供する方法について助けを見つけませんでした。私は、サーバーイメージをクライアントに送ることができるようにしたい。たとえば、クライアントが localhost:1313/Imagesのような要求を行う場合、サーバは "images"フォルダにあるすべての画像を表示するページで応答する必要があります。ここで
は、私がこれまで持っているコードです:import 'dart:io';
class Server {
_send404(HttpResponse res){
res.statusCode = HttpStatus.NOT_FOUND;
res.outputStream.close();
}
void startServer(String mainPath){
HttpServer server = new HttpServer();
server.listen('localhost', 1111);
print("Server listening on localhost, port 1111");
server.defaultRequestHandler = (var req, var res) {
final String path = req.path == '/' ? '/index.html' : req.path;
final File file = new File('${mainPath}${path}');
file.exists().then((bool found) {
if(found) {
file.fullPath().then((String fullPath) {
if(!fullPath.startsWith(mainPath)) {
_send404(res);
} else {
file.openInputStream().pipe(res.outputStream);
}
});
} else {
_send404(res);
}
});
};
void main(){
Server server = new Server();
File f = new File(new Options().script);
f.directory().then((Directory directory) {
server.startServer(directory.path);
});
}
私はまだクライアントを実装していないが、それは、クライアントを実装する必要があるのでしょうか?ブラウザはクライアントとして十分ではありませんか?
また、サーバーでイメージを配信するにはどうすればよいですか?
画像ファイル名や画像自体のリストを表示したいですか?後者の場合、何らかのHTMLテンプレートを実行する必要があります。 – tjameson