Class: WsClient::Client
- Inherits:
-
Object
- Object
- WsClient::Client
- Defined in:
- lib/ws_client.rb
Constant Summary collapse
- MS_2 =
(1/500.0)
- FRAME_SIZE =
2048
Instance Attribute Summary collapse
-
#connect_options ⇒ Object
readonly
Returns the value of attribute connect_options.
-
#message_queue ⇒ Object
readonly
Returns the value of attribute message_queue.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect(url = nil, options = {}) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #open? ⇒ Boolean
- #reconnect ⇒ Object
-
#send_data_and_wait(data, timeout = MS_2, opt = { :type => :text }) ⇒ Object
(also: #send_data)
Returns all responses that have been accepted.
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
22 23 24 |
# File 'lib/ws_client.rb', line 22 def initialize = ::Queue.new end |
Instance Attribute Details
#connect_options ⇒ Object (readonly)
Returns the value of attribute connect_options.
17 18 19 |
# File 'lib/ws_client.rb', line 17 def end |
#message_queue ⇒ Object (readonly)
Returns the value of attribute message_queue.
17 18 19 |
# File 'lib/ws_client.rb', line 17 def end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
17 18 19 |
# File 'lib/ws_client.rb', line 17 def url @url end |
Instance Method Details
#close ⇒ Object
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
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, ={}) return if open? = @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 = [:ssl_context] || begin ctx = OpenSSL::SSL::SSLContext.new ctx.ssl_version = [:ssl_version] || 'SSLv23' ctx.verify_mode = [: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
100 101 102 |
# File 'lib/ws_client.rb', line 100 def open? @handshake && @handshake.finished? && !@closed end |
#reconnect ⇒ Object
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) (@socket, timeout) if .length > 0 .length.times do response_data << .pop end end response_data end |