module Gemini::WebsocketConnection

Public Instance Methods

listen!() click to toggle source
# File lib/gemini/websocket_connection.rb, line 8
def listen!
  subscribe_to_channels
  listen
  ws_client.run!
end
ws_auth(&block) click to toggle source
# File lib/gemini/websocket_connection.rb, line 24
def ws_auth(&block)
  unless @ws_auth
    nonce = (Time.now.to_f * 10_000).to_i.to_s
    sub_id = add_callback(&block)
    save_channel_id(sub_id,0)
    if config.api_version == 1
      payload = 'AUTH' + nonce
      signature = sign(payload)
      ws_safe_send({
        apiKey: config.api_key,
        authSig: sign(payload),
        authPayload: payload,
        subId: sub_id.to_s,
        event: 'auth'
      })
    else
      payload = 'AUTH' + nonce + nonce
      signature = sign(payload)
      ws_safe_send({
        apiKey: config.api_key,
        authSig: sign(payload),
        authPayload: payload,
        authNonce: nonce,
        subId: sub_id.to_s,
        event: 'auth'
      })
    end
    @ws_auth = true
  end
end
ws_close_all() click to toggle source
# File lib/gemini/websocket_connection.rb, line 18
def ws_close_all
  ws_client.stop!
  @ws_open = false
  ws_reset_channels
end
ws_send(msg) click to toggle source
# File lib/gemini/websocket_connection.rb, line 14
def ws_send(msg)
  ws_client.send msg
end
ws_unauth() click to toggle source
# File lib/gemini/websocket_connection.rb, line 55
def ws_unauth
  ws_safe_send({event: 'unauth'})
end

Private Instance Methods

add_callback(&block) click to toggle source
# File lib/gemini/websocket_connection.rb, line 92
def add_callback(&block)
  id = 0
  @mutex.synchronize do
    callbacks[@c_counter] = { block: block, chan_id: nil }
    id = @c_counter
    @c_counter += 1
  end
  id
end
callbacks() click to toggle source
# File lib/gemini/websocket_connection.rb, line 88
def callbacks
  @callbacks ||= []
end
chan_ids() click to toggle source
# File lib/gemini/websocket_connection.rb, line 76
def chan_ids
  @chan_ids ||= []
end
exec_callback_for(msg) click to toggle source
# File lib/gemini/websocket_connection.rb, line 142
def exec_callback_for(msg)
  return if msg[1] == 'hb' #ignore heartbeat
  id = msg[0]
  callbacks[chan_ids[id.to_i]][:block].call(msg)
end
listen() click to toggle source
# File lib/gemini/websocket_connection.rb, line 126
def listen
  ws_client.on(:message) do |rmsg|
     msg = JSON.parse(rmsg)
     if msg.kind_of?(Hash) && msg["event"] == "subscribed"
       save_channel_id(msg["subId"],msg["chanId"])
     elsif msg.kind_of?(Array)
       exec_callback_for(msg)
     end
  end
end
register_authenticated_channel(msg, &block) click to toggle source
# File lib/gemini/websocket_connection.rb, line 102
def register_authenticated_channel(msg, &block)
  sub_id = add_callback(&block)
  msg.merge!(subId: sub_id.to_s)
  ws_safe_send(msg.merge(event:'subscribe'))
end
register_channel(msg, &block) click to toggle source
# File lib/gemini/websocket_connection.rb, line 116
def register_channel(msg, &block)
  sub_id = add_callback(&block)
  msg.merge!(subId: sub_id.to_s)
  if ws_open
    ws_client.send msg.merge(event: 'subscribe')
  else
    ws_registration_messages.push msg.merge(event: 'subscribe')
  end
end
save_channel_id(sub_id,chan_id) click to toggle source
# File lib/gemini/websocket_connection.rb, line 137
def save_channel_id(sub_id,chan_id)
  callbacks[sub_id.to_i][:chan_id] = chan_id
  chan_ids[chan_id] = sub_id.to_i
end
subscribe_to_channels() click to toggle source
# File lib/gemini/websocket_connection.rb, line 148
def subscribe_to_channels
  ws_client.on(:open) do
    @ws_open = true
    ws_registration_messages.each do |msg|
      ws_client.send(msg)
    end
  end
end
ws_client() click to toggle source
# File lib/gemini/websocket_connection.rb, line 67
def ws_client
  options = {
    url: config.websocket_api_endpoint,
    reconnect: config.reconnect,
    reconnect_after: config.reconnect_after
  }
  @ws_client ||= WSClient.new(options)
end
ws_open() click to toggle source
# File lib/gemini/websocket_connection.rb, line 80
def ws_open
  @ws_open ||= false
end
ws_registration_messages() click to toggle source
# File lib/gemini/websocket_connection.rb, line 84
def ws_registration_messages
  @ws_registration_messages ||= []
end
ws_reset_channels() click to toggle source
# File lib/gemini/websocket_connection.rb, line 61
def ws_reset_channels
  @chan_ids = []
  @ws_registration_messages = []
  @callbacks = {}
end
ws_safe_send(msg) click to toggle source
# File lib/gemini/websocket_connection.rb, line 108
def ws_safe_send(msg)
  if ws_open
    ws_client.send msg
  else
    ws_registration_messages.push msg
  end
end