Sha256: 5b6bb051458262faa161bda6d780a125e8a3f03363b9dd300016fe5483d2d437

Contents?: true

Size: 885 Bytes

Versions: 1

Compression:

Stored size: 885 Bytes

Contents

require 'mqtt'

module MQTTRpc
  class Client
    def initialize(options = {}, timeout = 10)
      @options = options
      @timeout = timeout
    end

    def publish(publish_topic, message, subscribe_topic)
      ready

      @subscribe_connect.subscribe(subscribe_topic)
      @publish_connect.publish(publish_topic, message)
      begin
        Timeout.timeout(@timeout) do
          @subscribe_connect.get do |_topic, payload|
            @payload = payload
            break
          end
        end
      rescue
        raise TimeoutException
      ensure
        destroy
      end

      @payload
    end

    private

    def connect
      MQTT::Client.connect(@options)
    end

    def ready
      @subscribe_connect = connect
      @publish_connect = connect
    end

    def destroy
      @subscribe_connect.disconnect
      @publish_connect.disconnect
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mqtt_rpc-0.3.0 lib/mqtt_rpc/client.rb