connect method

Future<bool> connect(
  1. String uri
)

Connect to the DTD at uri and register the rohd.goToSource service.

Returns true if connection and registration succeeded.

Implementation

Future<bool> connect(String uri) async {
  if (_disposed) {
    return false;
  }

  try {
    _channel = WebSocketChannel.connect(Uri.parse(uri));
    await _channel!.ready;
    _peer = Peer(_channel!.cast<String>());

    // Register the service method.
    _peer!.registerMethod('rohd.goToSource', _handleGoToSource);

    // Start listening (non-blocking).
    unawaited(
      _peer!.listen().then((_) {
        // Connection closed.
        _peer = null;
      }),
    );

    return true;
  } on Exception catch (e) {
    _peer = null;
    _channel = null;
    // DTD connections can run without a host-provided logger.
    // ignore: avoid_print
    print('[DtdService] Failed to connect to DTD at $uri: $e');
    return false;
  }
}