Exposed shared property HttpServer bind method to support more use-cases.

This commit is contained in:
Aleksandar Lugonja 2022-10-24 10:52:49 +02:00
parent 4973770c1c
commit 62df63de6c
1 changed files with 11 additions and 1 deletions

View File

@ -17,6 +17,7 @@ class InAppLocalhostServer {
bool _started = false;
HttpServer? _server;
int _port = 8080;
bool _shared = false;
String _directoryIndex = 'index.html';
String _documentRoot = './';
@ -25,15 +26,24 @@ class InAppLocalhostServer {
///- [directoryIndex] represents the index file to use. The default value is `index.html`.
///
///- [documentRoot] represents the document root path to serve. The default value is `./`.
///
///- The optional argument [shared] specifies whether additional `HttpServer`
/// objects can bind to the same combination of `address`, `port` and `v6Only`.
/// If `shared` is `true` and more `HttpServer`s from this isolate or other
/// isolates are bound to the port, then the incoming connections will be
/// distributed among all the bound `HttpServer`s. Connections can be
/// distributed over multiple isolates this way.
InAppLocalhostServer({
int port = 8080,
String directoryIndex = 'index.html',
String documentRoot = './',
bool shared = false,
}) {
this._port = port;
this._directoryIndex = directoryIndex;
this._documentRoot =
(documentRoot.endsWith('/')) ? documentRoot : '$documentRoot/';
this._shared = shared;
}
///Starts the server on `http://localhost:[port]/`.
@ -57,7 +67,7 @@ class InAppLocalhostServer {
var completer = Completer();
runZonedGuarded(() {
HttpServer.bind('127.0.0.1', _port).then((server) {
HttpServer.bind('127.0.0.1', _port, shared: _shared).then((server) {
print('Server running on http://localhost:' + _port.toString());
this._server = server;