Method: IB::Gateway#check_connection

Defined in:
lib/ib/gateway.rb

#check_connectionObject

Handy method to ensure that a connection is established and active.

The connection is reset on the IB-side at least once a day. Then the IB-Ruby-Connection has to be reestablished, too.

check_connection reconnects if necessary and returns false if the connection is lost.

It delays the process by 6 ms (150 MBit Cable connection)

a =  Time.now; G.check_connection; b= Time.now ;b-a
 => 0.00066005


420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/ib/gateway.rb', line 420

def check_connection
    q =  Queue.new 
    count = 0
    result = nil
    z= tws.subscribe( :CurrentTime ) { q.push true }
  loop do
    begin
      tws.send_message(:RequestCurrentTime)                        # 10 ms  ##
        th = Thread.new{ sleep 1 ; q.push nil }
        result =  q.pop 
        count+=1
        break if result || count > 10
    rescue IOError, Errno::ECONNREFUSED   # connection lost
      count +=1 
        retry
    rescue IB::Error # not connected
      reconnect 
      count = 0
      retry
    end
  end
  tws.unsubscribe z
  result #  return value
end