Module: Fcoin::RealTime::WSS

Included in:
EndPoint
Defined in:
lib/fcoin/realtime/wss.rb

Instance Method Summary collapse

Instance Method Details

#subscribeObject

Note:

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

Subscribe to the channel that you have added to the topics

Examples:

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}


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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