vmServiceConnectUriWithFactory<T extends VmService> function

Future<T> vmServiceConnectUriWithFactory<T extends VmService>(
  1. String wsUri, {
  2. required VmServiceFactory<T> vmServiceFactory,
  3. Log? log,
  4. Duration? pingInterval = const Duration(seconds: 15),
})

Connect to the given uri and return a new instance of T, which is constructed by vmServiceFactory and may be a subclass of VmService.

Implementation

Future<T> vmServiceConnectUriWithFactory<T extends VmService>(
  String wsUri, {
  required VmServiceFactory<T> vmServiceFactory,
  Log? log,
  // pingInterval has a default to act as a keep-alive to prevent proxies like
  // Norton antivirus from dropping connections with no traffic for a period.
  Duration? pingInterval = const Duration(seconds: 15),
}) async {
  final WebSocket socket = await WebSocket.connect(wsUri)
    ..pingInterval = pingInterval;
  final StreamController<dynamic> controller = StreamController();
  final Completer streamClosedCompleter = Completer();

  socket.listen(
    (data) => controller.add(data),
    onDone: () => streamClosedCompleter.complete(),
  );

  return vmServiceFactory(
    inStream: controller.stream,
    writeMessage: (String message) => socket.add(message),
    log: log,
    disposeHandler: () => socket.close(),
    streamClosed: streamClosedCompleter.future,
    wsUri: wsUri,
  );
}