class PeatioAPI::StreamingClient

Public Class Methods

new(options={}) click to toggle source
Calls superclass method PeatioAPI::Client::new
# File lib/peatio_api/streaming_client.rb, line 8
def initialize(options={})
  super
  @endpoint = options[:endpoint] || 'wss://peatio.com:8080'
  @logger   = options[:logger] || Logger.new(STDOUT)
end

Public Instance Methods

run(&callback) click to toggle source
# File lib/peatio_api/streaming_client.rb, line 14
def run(&callback)
  EM.run do
    ws = Faye::WebSocket::Client.new(@endpoint)

    ws.on(:open) do |event|
      @logger.info "Connected."
    end

    ws.on(:message) do |event|
      msg = JSON.parse(event.data)

      key = msg.keys.first
      data = msg[key]
      case key
      when 'challenge'
        ws.send JSON.dump(@auth.signed_challenge(data))
      else
        begin
          callback.call msg
        rescue
          @logger.error "Failed to process message: #{payload}"
          @logger.error $!
        end
      end
    end

    ws.on(:close) do |event|
      @logger.info "Closed. Code: #{event.code}, Reason: #{event.reason || 'none'}"
      ws = nil
      EM.stop
    end

  end

end