sendMessage method

dynamic sendMessage({
  1. required MessageAction action,
  2. required String message,
  3. String? target,
})

Implementation

sendMessage(
    {required MessageAction action,
    required String message,
    String? target}) async {
  Completer completer = Completer();
  try {
    if (_state == CallState.nullState || _state == CallState.sessionState) {
      completer.completeError(
          {'error': 'The state that cannot send message, state $_state'});
      return;
    }

    if (action == MessageAction.whisper && target == null) {
      completer
          .completeError({'error': 'Invalid request, no target to whisper'});
      return;
    }

    if (message.length > 2048) {
      completer.completeError({'error': 'Too long message'});
      return;
    }
    if (target != null && target.length > 128) {
      completer.completeError({'error': 'Too long message'});
      return;
    }

    if (_ws.channel?.closeCode != null) {
      completer
          .completeError({'error': 'Invalid websocket connection state'});
      return;
    }

    await _ws.requestMessage(MessageAction.send, message, target);
    completer.complete();
    return completer.future;
  } catch (error) {
    completer.completeError(error);
  }
}