startServer method
Starts listening to requests and filesystem events.
Implementation
Future<HttpServer> startServer(
[String address = '127.0.0.1', int port = 3000]) async {
var isHot = true;
_server = await _generateServer();
if (_paths.isNotEmpty != true) {
_logWarning(
'You have instantiated a HotReloader without providing any filesystem paths to watch.');
}
bool sw(String s) {
return Platform.executableArguments.any((ss) => ss.startsWith(s));
}
if (!sw('--observe') && !sw('--enable-vm-service')) {
_logWarning(
'You have instantiated a HotReloader without passing `--enable-vm-service` or `--observe` to the Dart VM. Hot reloading will be disabled.');
isHot = false;
} else {
var info = await dev.Service.getInfo();
var uri = info.serverUri!;
uri = uri.replace(path: p.join(uri.path, 'ws'));
if (uri.scheme == 'https') {
uri = uri.replace(scheme: 'wss');
} else {
uri = uri.replace(scheme: 'ws');
}
_client = await vm.vmServiceConnectUri(uri.toString());
_vmachine ??= await _client.getVM();
_mainIsolate ??= _vmachine?.isolates?.first;
if (_vmachine != null) {
for (var isolate in _vmachine?.isolates ?? <vm.IsolateRef>[]) {
var isolateId = isolate.id;
if (isolateId != null) {
await _client.setIsolatePauseMode(isolateId,
exceptionPauseMode: 'None');
}
}
}
await _listenToFilesystem();
}
_onChange.stream
//.transform( _Debounce( Duration(seconds: 1)))
.listen(_handleWatchEvent);
while (_requestQueue.isNotEmpty) {
await _handle(_requestQueue.removeFirst());
}
var server = _io = await HttpServer.bind(address, port);
//server.defaultResponseHeaders();
server.listen(handleRequest);
// Print a Flutter-like prompt...
if (enableHotkeys) {
var serverUri =
Uri(scheme: 'http', host: server.address.address, port: server.port);
Uri? observatoryUri;
if (isHot) {
observatoryUri = await dev.Service.getInfo().then((i) => i.serverUri);
}
print(styleBold.wrap(
'\n🔥 To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".'));
stdout.write('Your server is listening at: ');
print(wrapWith('$serverUri', [cyan, styleUnderlined]));
if (isHot) {
stdout.write(
'An Observatory debugger and profiler on ${Platform.operatingSystem} is available at: ');
print(wrapWith('$observatoryUri', [cyan, styleUnderlined]));
} else {
stdout.write(
'The observatory debugger and profiler are not available.\n');
}
print(
'For a more detailed help message, press "h". To quit, press "q".\n');
if (_paths.isNotEmpty) {
print(darkGray.wrap(
'Changes to the following path(s) will also trigger a hot reload:'));
for (var p in _paths) {
print(darkGray.wrap(' * $p'));
}
stdout.writeln();
}
// Listen for hotkeys
try {
stdin.lineMode = stdin.echoMode = false;
} catch (_) {}
late StreamSubscription<int> sub;
try {
sub = stdin.expand((l) => l).listen((ch) async {
if (ch == $r) {
_handleWatchEvent(
WatchEvent(ChangeType.MODIFY, '[manual-reload]'), isHot);
}
if (ch == $R) {
_logInfo('Manually restarting server...\n');
await _killServer();
await _server?.close();
var addr = _io.address.address;
var port = _io.port;
await _io.close(force: true);
await startServer(addr, port);
} else if (ch == $q) {
stdin.echoMode = stdin.lineMode = true;
await close();
await sub.cancel();
exit(0);
} else if (ch == $h) {
print(
'Press "r" to hot reload the Dart VM, and restart the active server.');
print(
'Press "R" to restart the server, WITHOUT a hot reload of the VM.');
print('Press "q" to quit the server.');
print('Press "h" to display this help information.');
stdout.writeln();
}
});
} catch (_) {}
}
return server;
}