lib/deribit/ws.rb in deribit-1.3.2 vs lib/deribit/ws.rb in deribit-1.4.3

- old
+ new

@@ -1,17 +1,16 @@ require 'websocket-client-simple' module Deribit class WS AVAILABLE_EVENTS = [:order_book, :trade, :my_trade, :user_order, :index, :portfolio, :announcement] + attr_reader :socket, :response, :ids_stack, :handler, :subscribed_instruments, :test_server - attr_reader :socket, :response, :ids_stack, :handler, :subscribed_instruments - def initialize(key, secret, handler: Handler, test_server: nil) - test_server = ENV["DERIBIT_TEST_SERVER"] if test_server == nil + @test_server = test_server || ENV["DERIBIT_TEST_SERVER"] @request = Request.new(key, secret, test_server: test_server) - @socket = connect(test_server ? WS_TEST_URL : WS_SERVER_URL) + @socket = connect @handler = handler.instance_of?(Class) ? handler.new : handler @ids_stack = [] # the structure of subscribed_instruments: {'event_name' => ['instrument1', 'instrument2']]} @subscribed_instruments = {} @@ -30,11 +29,12 @@ instruments.uniq end end end - def connect(url) + def connect + url = test_server ? WS_TEST_URL : WS_SERVER_URL puts "Connecting to #{url}" WebSocket::Client::Simple.connect(url) end def reconnect! @@ -195,11 +195,11 @@ send(path: '/api/v1/private/cancelall', arguments: params) end def set_heartbeat(interval = "60") params = { - "interval": interval + "interval": interval.to_s } send(path: '/api/v1/public/setheartbeat', arguments: params) end @@ -219,10 +219,11 @@ # puts "msg = #{msg.inspect}" begin if msg.type == :text json = JSON.parse(msg.data, symbolize_names: true) puts "Subscribed!" if json[:message] == "subscribed" + # puts "Got json: #{json}" if json[:message] == "test_request" # puts "Got test request: #{json.inspect}" # DEBUG instance.test elsif json[:id] and stack_id = instance.ids_stack.find{|i| i[json[:id]]} @@ -241,10 +242,11 @@ instance.handle_notifications(json[:notifications]) else instance.handler.send(:notice, json) end - instance.handler.timestamp = Time.now.to_i + # puts "Want to update timestamp for handler for event: #{json.inspect}" + instance.handler.update_timestamp! elsif msg.type == :close puts "trying to reconnect = got close event, msg: #{msg.inspect}" instance.reconnect! end rescue StandardError => e