sendMessage method

  1. @override
Future<String?> sendMessage(
  1. String json, {
  2. Duration? replyTimeout,
})
override

Sends a live message. With a null replyTimeout it is fire-and-forget and resolves with null. Otherwise it waits for the watch to reply and resolves with the reply JSON (null for an empty reply), or throws WatchTimeoutException.

Implementation

@override
Future<String?> sendMessage(String json, {Duration? replyTimeout}) async {
  _requireLive();
  sentMessages.add(json);
  final peer = _peer;
  if (peer == null) {
    return replyTimeout == null ? null : replyBuilder?.call(json);
  }
  final id = 'msg-${_nextId++}';
  peer._messageCtl.add(RawMessage(id, json, expectsReply: replyTimeout != null));
  if (replyTimeout == null) return null;
  final completer = peer._pendingReplies[id] = Completer<String?>();
  return completer.future.timeout(replyTimeout, onTimeout: () {
    peer._pendingReplies.remove(id);
    throw const WatchTimeoutException('No reply from the watch');
  });
}