Class: Stomp::Message

### Home Classes Methods

### In Files

- lib/stomp/message.rb

### Parent

Object

### Methods

- ::new
- #to_s

### Files

- CHANGELOG.rdoc
- LICENSE

### Class/Module Index

Quicksearch

- Stomp
- Stomp::Client
- Stomp::Connection
- Stomp::Error
- Stomp::Error::ConnectReadTimeout
- Stomp::Error::DuplicateSubscription
- Stomp::Error::InvalidFormat
- Stomp::Error::InvalidHeartBeatHeaderError
- Stomp::Error::InvalidMessageLength
- Stomp::Error::InvalidServerCommand
- Stomp::Error::LoggerConnectionError
- Stomp::Error::MalformedFailoverOptionsError
- Stomp::Error::MaxReconnectAttempts
- Stomp::Error::MessageIDRequiredError
- Stomp::Error::NilMessageError
- Stomp::Error::NoCurrentConnection
- Stomp::Error::PacketParsingTimeout
- Stomp::Error::ProtocolError11p
- Stomp::Error::ProtocolErrorConnect
- Stomp::Error::SSLClientParamsError
- Stomp::Error::SSLNoCertFileError
- Stomp::Error::SSLNoKeyFileError
- Stomp::Error::SSLNoTruststoreFileError
- Stomp::Error::SSLUnreadableCertFileError
- Stomp::Error::SSLUnreadableKeyFileError
- Stomp::Error::SSLUnreadableTruststoreFileError
- Stomp::Error::SocketOpenTimeout
- Stomp::Error::StompServerError
- Stomp::Error::SubscriptionRequiredError
- Stomp::Error::UTF8ValidationError
- Stomp::Error::UnsupportedProtocolError
- Stomp::HeaderCodec
- Stomp::Message
- Stomp::SSLParams
- Client11Example1
- Client11PutGet1
- Connection11Example1
- Connection11Example2
- ExampleConsumer
- ExamplePublisher
- ExampleRubyCiphers
- ExampleSSL1
- ExampleSSL1C
- ExampleSSL2
- ExampleSSL2C
- ExampleSSL3
- ExampleSSL3C
- ExampleSSL4
- ExampleSSL4C
- ExampleTopicConsumer
- ExampleTopicPublisher
- Hash
- HeartBeatExample1
- LoggerExample
- Object
- Publish11Example1
- Receive11Example1
- Receive11Example2
- RepeatedHeadersExample
- SSLLoggerExample
- Slogger
- Stomp11Common
- TestBase
- TestClient
- TestConnection
- TestConnection1P
- TestMessage
- TestSSL
- TestURLLogins
- Tlogger

No matching classes.

# Stomp::Message

Message is a container class for frames. Misnamed technically.

### Attributes

body[RW]

The message Body.

command[RW]

The COMMAND value.

headers[RW]

The Headers Hash.

original[RW]

The original input(s).

### Public Class Methods

new(frame, protocol11p = false) click to toggle source

initialize returns a Message from a raw physical frame.

    # File lib/stomp/message.rb, line 25
    def initialize(frame, protocol11p = false)
      # p [ "00", frame, frame.encoding ]
      # Set default empty values
      self.command = ''
      self.headers = {}
      self.body = ''
      self.original = frame
      return self if is_blank?(frame)
      # Figure out where individual parts of the frame begin and end.
      command_index = frame.index("\n")
      raise Stomp::Error::InvalidFormat, 'command index' unless command_index
      #
      headers_index = frame.index("\n\n", command_index+1)
      raise Stomp::Error::InvalidFormat, 'headers index' unless headers_index
      #
      lastnull_index = frame.rindex("\00"")
      raise Stomp::Error::InvalidFormat, 'lastnull index' unless lastnull_index

      # Extract working copies of each frame part
      work_command = frame[0..command_index-1]
      raise Stomp::Error::InvalidServerCommand, "invalid command: #{work_command.inspect}" unless @@allowed_commands.include?(work_command)
      #
      work_headers = frame[command_index+1..headers_index-1]
      raise Stomp::Error::InvalidFormat, 'nil headers' unless work_headers
      #
      work_body = frame[headers_index+2..lastnull_index-1]
      raise Stomp::Error::InvalidFormat, 'nil body' unless work_body
      # Set the frame values
      if protocol11p
        work_command.force_encoding(Stomp::UTF8) if work_command.respond_to?(:force_encoding)
      end
      self.command = work_command
      work_headers.split("\n").map do |value|
        fc = value.index(":")
        raise Stomp::Error::InvalidFormat, 'parsed header value' unless fc
        #
        pk = value[0...fc]
        pv = value[fc+1..-1]
        #
        if protocol11p
          pk.force_encoding(Stomp::UTF8) if pk.respond_to?(:force_encoding)
          pv.force_encoding(Stomp::UTF8) if pv.respond_to?(:force_encoding)
          # Stomp 1.1+ - Servers may put multiple values for a single key on the wire.
          # If so, we support reading those, and passing them to the user.
          if self.headers[pk]
            if self.headers[pk].is_a?(Array) # The 3rd and any subsequent ones for this key
              self.headers[pk] << pv
            else
              # The 2nd one for this key
              tv = self.headers[pk] + ""
              self.headers[pk] = []
              self.headers[pk] << tv << pv
            end
          else
            self.headers[pk] = pv # The 1st one for this key
          end
        else
          # Stomp 1.0
          self.headers[pk.strip] = pv.strip unless self.headers[pk.strip] # Only receive the 1st one
        end
      end

      body_length = -1

      if self.headers['content-length']
        body_length = self.headers['content-length'].to_i
        raise Stomp::Error::InvalidMessageLength if work_body.length != body_length
      end
      self.body = work_body[0..body_length]
    end

### Public Instance Methods

to_s() click to toggle source

to_s returns a string prepresentation of this Message.

    # File lib/stomp/message.rb, line 97
    def to_s
      "<Stomp::Message headers=#{headers.inspect} body='#{body}' command='#{command}' >"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.