module BitShares::WSocket
Public Class Methods
get_notice()
click to toggle source
# File lib/bitshares/wsocket.rb, line 64 def get_notice if msgs.key?('notice') && !@msgs['notice'].nil? msg = @msgs['notice'] lock.synchronize do @msgs['notice'] = nil end msg else nil end end
lock()
click to toggle source
# File lib/bitshares/wsocket.rb, line 62 def lock() @lock ||= Mutex.new end
msgs()
click to toggle source
# File lib/bitshares/wsocket.rb, line 26 def msgs() @msgs ||= {} end
send(hash)
click to toggle source
# File lib/bitshares/wsocket.rb, line 32 def send hash id = hash[:id] count = 0 while msgs[id] != nil do sleep 0.1 count += 1 raise "TimeoutError..." if count > 100 end if @status == :connected @thread[:ws].send hash.to_json else return nil end count = 0 while @msgs[id].nil? and @status == :connected do sleep 0.1 count += 1 raise "TimeoutError..." if count > 100 end result = @msgs[id] lock.synchronize do @msgs[id] = nil end raise("Bad request...#{result}") if result.nil? || result.key?('error') result['result'] end
start()
click to toggle source
# File lib/bitshares/wsocket.rb, line 8 def start @msgs = {} @thread ||= Thread.new { run } Thread.new do while @status != :disconnected do @thread[:ws].ping 'Hi' if @status == :connected sleep 50 end end count = 0 while @status != :connected and @status != :disconnected do sleep 0.1 count += 1 raise "TimeoutError..." if count > 100 end end
stop()
click to toggle source
# File lib/bitshares/wsocket.rb, line 28 def stop EventMachine.stop end
thread()
click to toggle source
# File lib/bitshares/wsocket.rb, line 25 def thread() @thread end
Private Class Methods
run()
click to toggle source
# File lib/bitshares/wsocket.rb, line 77 def run begin @status ||= :start EM.run do ws = WebSocket::EventMachine::Client.connect(:uri => BitShares.node) Thread.current[:ws] = ws ws.onopen do if @status == :start puts "Connected" @status = :connected else EventMachine.stop end end ws.onmessage do |msg, type| json = JSON.parse msg id = json['id'] || json['method'] lock.synchronize do @msgs[id] = json end end ws.onclose do |code, reason| puts "Disconnected with status code: #{code}" @status = :disconnected EventMachine.stop end ws.onpong do |msg| #puts "Pong #{msg}" end end rescue Exception => e puts e.message end end