listen method
Start the server by listening to the specified host
and port
.
Can be called in two ways.
await app.listen(port, () { });
await app.listen(port, host, () { });
Default host is localhost and port is 3000.
Implementation
Future<void> listen(int port, [host, void Function()? onReady]) async {
// default host and port
String defaultHost = 'localhost';
// app.listen(3000);
if (host == null && onReady == null) {
host = defaultHost;
}
// app.listen(3000, () { });
else if (host is void Function() && onReady == null) {
onReady = host;
host = defaultHost;
}
assert(port is int && host is String, 'Invalid parameter');
if (_useSecure) {
// bind https server to specified port
_server = await HttpServer.bindSecure(
host,
port,
_securityContext!,
shared: shared,
);
} else {
// bind server to specified port
_server = await HttpServer.bind(
host,
port,
shared: shared,
);
}
// set custom idleTimeout
if (idleTimeout is Duration) {
_server.idleTimeout = idleTimeout;
}
// start listening for incoming requests
_server.listen((HttpRequest request) {
// add each request to queue for processing
_requestQueue.add(() async {
await _processRequest(request);
});
});
// save onReady for restart
_onReady = onReady;
// inform server is ready
onReady?.call();
}