Class: MKIt::WebSocketClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mkit/client/websocket_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(server_url, my_id) ⇒ WebSocketClient

Returns a new instance of WebSocketClient.



12
13
14
15
16
17
18
19
20
21
# File 'lib/mkit/client/websocket_client.rb', line 12

def initialize(server_url, my_id)
  @server_url = server_url
  @my_id = my_id
  uri = URI(@server_url)
  use_ssl = uri.scheme == 'https'
  @options = use_ssl ? { tls: { :verify_peer => false } } : {}
  @options[:headers] = { 'X-API-KEY' => @my_id }
  url_prefix = use_ssl ? "wss" : "ws"
  @ws_url = "#{url_prefix}://#{uri.host}:#{uri.port}"
end

Instance Method Details

#request(request, request_data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mkit/client/websocket_client.rb', line 23

def request(request, request_data)
  uri = request[:uri]
  unless request[:params].nil? || request[:params].empty?
    uri = uri + '?' + request[:params].map { |k| "#{k}=#{request_data[k]}" }.join('&')
  end
  uri = ERB.new("#{@ws_url}#{uri}").result_with_hash(request_data)

  case request[:verb].to_sym
  when :ws_console
    client = ConsoleWebSocketClient.new(uri, @options)
  when :ws
    client = LogWebSocketClient.new(uri, @options)
  end
  client.doIt

end