keepAliveSweep method

Future<void> keepAliveSweep({
  1. Duration timeout = const Duration(seconds: 4),
})

Active keepalive + liveness for transient stream transports (ble:// / tcp:// / serial:// carried on a streamableHttp config). Sends a cheap listResources round-trip on every such CONNECTED link:

  • the traffic keeps the link warm — an idle BLE session to an ESP32 drops in ~15s, but ~2-3s keepalive traffic stretches it to ~45s (measured), so far fewer reconnect cycles;
  • a probe that fails/times out means the link died silently, so the entry is marked error (via _handleTransportDrop) and the health monitor reconnects it. HTTP/SSE/stdio servers are skipped — they don't suffer idle-drop and a periodic poll would just be noise.

Implementation

Future<void> keepAliveSweep({
  Duration timeout = const Duration(seconds: 4),
}) async {
  final targets = _connections.entries
      .where((e) =>
          e.value.state == ConnectionState.connected &&
          e.value.client != null &&
          _isTransientStream(e.value.serverConfig))
      .toList();
  for (final e in targets) {
    final client = e.value.client!;
    try {
      await client.listResources().timeout(timeout);
    } catch (_) {
      _handleTransportDrop(e.key, DisconnectReason.transportError);
    }
  }
}