connect static method

Future<ConnectClient> connect(
  1. String port,
  2. String secret
)

Implementation

static Future<ConnectClient> connect(String port, String secret) async {
  final wsUrl = Uri.parse('ws://127.0.0.1:$port/$secret=/ws');
  final channel = WebSocketChannel.connect(wsUrl);

  // ignore: avoid_print
  final stream = channel.stream.handleError(print);

  final service = VmService(
    stream,
    channel.sink.add,
    disposeHandler: channel.sink.close,
  );
  final vm = await service.getVM();
  final isolateId = vm.isolates!.where((e) => e.name == 'main').first.id!;
  await service.streamListen(EventStreams.kExtension);

  final client = ConnectClient(service, isolateId);
  final handlers = {
    ConnectEvent.instancesChanged.event: (_) {
      client._instancesChangedController.add(null);
    },
    ConnectEvent.collectionInfoChanged.event: (Map<String, dynamic> json) {
      final collectionInfo = ConnectCollectionInfoPayload.fromJson(json);
      client.collectionInfo[collectionInfo.collection] = collectionInfo;
      client._collectionInfoChangedController.add(null);
    },
    ConnectEvent.queryChanged.event: (_) {
      client._queryChangedController.add(null);
    },
  };
  service.onExtensionEvent.listen((Event event) {
    final data = event.extensionData?.data ?? {};
    handlers[event.extensionKind]?.call(data);
  });

  return client;
}