Class: WsClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ws_client.rb

Constant Summary collapse

MS_2 =
(1/500.0)
FRAME_SIZE =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



22
23
24
# File 'lib/ws_client.rb', line 22

def initialize
  @message_queue = ::Queue.new
end

Instance Attribute Details

#connect_optionsObject (readonly)

Returns the value of attribute connect_options.



17
18
19
# File 'lib/ws_client.rb', line 17

def connect_options
  @connect_options
end

#message_queueObject (readonly)

Returns the value of attribute message_queue.



17
18
19
# File 'lib/ws_client.rb', line 17

def message_queue
  @message_queue
end

#urlObject (readonly)

Returns the value of attribute url.



17
18
19
# File 'lib/ws_client.rb', line 17

def url
  @url
end

Instance Method Details

#closeObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ws_client.rb', line 84

def close
  return if @closed

  write_data nil, :type => :close rescue nil
ensure
  @closed = true
  @socket.close if @socket
  @tcp_socket.close if @tcp_socket
  @socket = nil
  @tcp_socket = nil
end

#closed?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/ws_client.rb', line 96

def closed?
  !open?
end

#connect(url = nil, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ws_client.rb', line 26

def connect(url = nil, options={})
  return if open?

  @connect_options = options
  @connect_url = @url = url || @connect_url || @url

  raise "No URL to connect to" if url.nil?

  uri = URI.parse url
  @socket = TCPSocket.new(uri.host, uri.port || (uri.scheme == 'wss' ? 443 : 80))
  @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

  if ['https', 'wss'].include?(uri.scheme)
    ssl_context = options[:ssl_context] || begin
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.ssl_version = options[:ssl_version] || 'SSLv23'
    ctx.verify_mode = options[:verify_mode] || OpenSSL::SSL::VERIFY_NONE #use VERIFY_PEER for verification
    cert_store = OpenSSL::X509::Store.new
    cert_store.set_default_paths
    ctx.cert_store = cert_store
    ctx
    end

    # Keep a handle to the TCPSocket, because the SSLSocket will not clean it up for us.
    @tcp_socket = @socket
    @socket = ::OpenSSL::SSL::SSLSocket.new(@tcp_socket, ssl_context)
    @socket.connect
  end

  @closed = false

  handshake
rescue OpenSSL::SSL::SSLError, EOFError
  # Re-use the socket cleanup logic if we have a connect failure.
  close
  raise
end

#open?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/ws_client.rb', line 100

def open?
  @handshake && @handshake.finished? && !@closed
end

#reconnectObject



64
65
66
# File 'lib/ws_client.rb', line 64

def reconnect
  connect(nil)
end

#send_data_and_wait(data, timeout = MS_2, opt = { :type => :text }) ⇒ Object Also known as: send_data

Returns all responses that have been accepted



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ws_client.rb', line 69

def send_data_and_wait(data, timeout = MS_2, opt = { :type => :text })
  response_data = []
  write_data(data, opt)
  pull_next_message_off_of_socket(@socket, timeout)

  if message_queue.length > 0
    message_queue.length.times do
      response_data << message_queue.pop
    end
  end

  response_data
end