StompMessage.fromFrame constructor

StompMessage.fromFrame(
  1. StompFrame frame
)

Creates a StompMessage from a MESSAGE frame

Implementation

factory StompMessage.fromFrame(StompFrame frame) {
  if (frame.command != StompCommands.message) {
    throw StompFrameException('Cannot create StompMessage from non-MESSAGE frame: ${frame.command}');
  }

  final messageId = frame.getHeader(StompHeaders.messageId);
  final destination = frame.getHeader(StompHeaders.destination);
  final subscriptionId = frame.getHeader(StompHeaders.subscription);

  if (messageId == null) {
    throw const StompFrameException('MESSAGE frame missing message-id header');
  }
  if (destination == null) {
    throw const StompFrameException('MESSAGE frame missing destination header');
  }
  if (subscriptionId == null) {
    throw const StompFrameException('MESSAGE frame missing subscription header');
  }

  return StompMessage(
    messageId: messageId,
    destination: destination,
    subscriptionId: subscriptionId,
    headers: Map<String, String>.from(frame.headers),
    body: frame.getBodyAsString(),
    ackId: frame.getHeader(StompHeaders.ack),
  );
}