module Fcoin::RealTime::WSS

Public Instance Methods

subscribe() click to toggle source

Subscribe to the channel that you have added to the topics

@note Please subscribe to the channel by calling client.on_hello(&block) for the first time

@example Subscribe to Subscribe to server time

client = Fcoin::Client.new.realtime
# client = Fcoin::RealTime::Client.new
client.on_hello do |data|
  puts data
end
client.subscribe
#=> {"type"=>"hello", "ts"=>1532953247264}
# File lib/fcoin/realtime/wss.rb, line 21
def subscribe
  EM.run do
    wss = Faye::WebSocket::Client.new(wss_endpoint)

    wss.on(:open) do |handshake|
      self.topics.each do |args|
        wss.send(valid_payload(args))
      end
    end

    wss.on(:message) do |event|
      data       = JSON.parse(event.data)
      topic = data["type"]
      formatter  = Fcoin::RealTime::Formatter.new(data)
      call_callbacks(topic, formatter.formatted_data)
    end

    wss.on(:close) do |event|
      call_callbacks(:close)
      EM.stop
    end

    wss.on(:error) do |event|
      call_callbacks(:error)
      EM.stop
    end

    # hit Control + C to stop
    Signal.trap("INT")  { EM.stop }
    Signal.trap("TERM") { EM.stop }
  end
end

Private Instance Methods

call_callbacks(topic, data={}) click to toggle source

call callbacks

@param topic [String] Channel you want to subscribe to @param data [Hash] Data sent from subscribed channel

# File lib/fcoin/realtime/wss.rb, line 76
def call_callbacks(topic, data={})
  return unless on?(topic)
  callbacks[topic].each do |callback|
    callback.call formatted(data)
  end
end
formatted(data) click to toggle source

change the output format of the body

@param data [Hash] @return [Hash or JSON]

# File lib/fcoin/realtime/wss.rb, line 87
def formatted(data)
  case format_type
  when :json
    data.to_json
  when :hash
    data
  else
    raise "format_type is #{format_type}. format_type must be included in [:json, :hash]."
  end
end
on(topic, limit=nil, &block) click to toggle source

Subscribe to topic

@param topic [String or Symbol] Channel you want to subscribe to @param limit [Integer]

# File lib/fcoin/realtime/wss.rb, line 59
def on(topic, limit=nil, &block)
  self.topics << { topic: topic, limit: limit }
  self.callbacks[topic] ||= []
  self.callbacks[topic] << block if block_given?
end
on?(topic) click to toggle source

Subscribe topic?

@param topic [String] Channel you want to subscribe to

# File lib/fcoin/realtime/wss.rb, line 68
def on?(topic)
  topic.present? && callbacks[topic].present?
end
valid_payload(args) click to toggle source

Prepare a valid payload

@param args [Hash] Parameters to send to subscribed channel

# File lib/fcoin/realtime/wss.rb, line 101
def valid_payload(args)
  topic = args[:topic]
  limit = args[:limit]
  payload = if limit.present?
              { cmd: :sub, args: [topic, limit], id: SecureRandom.uuid }
            else
              { cmd: :sub, args: [topic], id: SecureRandom.uuid }
            end
  JSON.dump(payload)
end