I'm having an issue with a Dart server. I'm trying to send multiple clients some information, but I don't have the information available for them right when they request it. That means I am using a Future
and when that Future
is complete, I will then send the data back to the client.
The problem, is that the server will not allow a second (or more) client to connect to the server while the first Future
is still waiting to be completed.
Here's an example:
import "dart:async";
import "dart:io";
import "package:route/server.dart";
void main(List<String> args) {
HttpServer.bind("localhost", 5000)
.then((HttpServer server) {
Router router = new Router(server)
..serve("/multi").listen(_multi);
});
}
void _multi(HttpRequest request) {
print("Waiting");
new Timer(new Duration(seconds: 5), () {
print("Replying");
request.response.write("Hello There");
request.response.close();
});
}
Basically, if you only have one client connect at a time, this works perfectly fine. If, however, you have more than one client, the first client that connects will block the second client until the first client's connection has been closed.
I've also tried to use ..serve("/multi").asBroadcastStream()...
, because I thought that would allow for multiple subscribers, but that had the same behavior.
Is there a way to do this?
Thanks.