sendMessage method

Future<void> sendMessage(
  1. Message message
)

Send a message to the client

Implementation

Future<void> sendMessage(Message message) async {
  if (_isClosed) {
    return _addSessionEvent(
      SessionEventGeneric(
        sessionId: id,
        type: SessionEventType.error,
        message: 'Session is closed',
      ),
    );
  }

  try {
    final data = _messageCodec.encode(message);

    if (data == null) {
      _addSessionEvent(
        SessionEventGeneric(
          sessionId: id,
          type: SessionEventType.error,
          message: 'Failed to encode message: $message. '
              'This message is not supported by any plugin.',
        ),
      );
      return;
    }

    await _connection.send(data);
  } catch (e) {
    _addSessionEvent(
      SessionEventGeneric(
        sessionId: id,
        type: SessionEventType.error,
        message: 'Failed to send message: $e',
      ),
    );

    // If we can't send, assume connection is dead
    _closeSession(reason: 'Failed to send message: $e');
    rethrow;
  }
}