Class: WsClient::Client

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

Direct Known Subclasses

AsyncClient

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.



24
25
26
# File 'lib/ws_client.rb', line 24

def initialize
  @message_queue = ::Queue.new
end

Instance Attribute Details

#connect_optionsObject (readonly)

Returns the value of attribute connect_options.



19
20
21
# File 'lib/ws_client.rb', line 19

def connect_options
  @connect_options
end

#message_queueObject (readonly)

Returns the value of attribute message_queue.



19
20
21
# File 'lib/ws_client.rb', line 19

def message_queue
  @message_queue
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/ws_client.rb', line 19

def url
  @url
end

Instance Method Details

#closeObject



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

def close
  return if @closed

  write_data nil, :type => :close if !@pipe_broken
  emit :close
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



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
# File 'lib/ws_client.rb', line 28

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

  @pipe_broken = false
  @closed = false

  handshake
end

#open?Boolean

Returns:

  • (Boolean)


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

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

#reconnectObject



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

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



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

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