Class: Kontena::Websocket::Client::Connection
- Inherits:
-
Object
- Object
- Kontena::Websocket::Client::Connection
- Includes:
- Waitable, Waitable_Ruby2_2, Logging
- Defined in:
- lib/kontena/websocket/client/connection.rb
Overview
WebSocket::Driver.client(…) API
Defined Under Namespace
Modules: Waitable, Waitable_Ruby2_2
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#initialize(uri, socket, write_timeout: nil) ⇒ Connection
constructor
A new instance of Connection.
-
#nonblocking_timeout(timeout = nil, &block) ⇒ Object
Wait up to timeout before retrying any blocking operation.
-
#read(size, timeout: nil) ⇒ String
0..size bytes.
- #url ⇒ String
- #write(buf) ⇒ Object
Methods included from Waitable_Ruby2_2
#wait_socket_readable!, #wait_socket_writable!
Methods included from Waitable
#wait_socket_readable!, #wait_socket_writable!
Methods included from Logging
#debug, #error, #info, initialize_logger, #logger, logger, logger=, #logging_prefix, #warn
Constructor Details
#initialize(uri, socket, write_timeout: nil) ⇒ Connection
Returns a new instance of Connection.
69 70 71 72 73 |
# File 'lib/kontena/websocket/client/connection.rb', line 69 def initialize(uri, socket, write_timeout: nil) @uri = uri @socket = socket @write_timeout = write_timeout end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
64 65 66 |
# File 'lib/kontena/websocket/client/connection.rb', line 64 def uri @uri end |
Instance Method Details
#nonblocking_timeout(timeout = nil, &block) ⇒ Object
Wait up to timeout before retrying any blocking operation.
84 85 86 87 88 89 90 91 92 |
# File 'lib/kontena/websocket/client/connection.rb', line 84 def nonblocking_timeout(timeout = nil, &block) return yield rescue IO::WaitReadable wait_socket_readable!(@socket, timeout) # raises Kontena::Websocket::TimeoutError retry rescue IO::WaitWritable wait_socket_writable!(@socket, timeout) # raises Kontena::Websocket::TimeoutError retry end |
#read(size, timeout: nil) ⇒ String
Returns 0..size bytes.
98 99 100 101 102 103 104 105 106 |
# File 'lib/kontena/websocket/client/connection.rb', line 98 def read(size, timeout: nil) buf = nonblocking_timeout(timeout) do @socket.read_nonblock(size) end debug "read size=#{size}: #buf=#{buf.size}" return buf end |
#url ⇒ String
76 77 78 |
# File 'lib/kontena/websocket/client/connection.rb', line 76 def url @uri.to_s end |
#write(buf) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/kontena/websocket/client/connection.rb', line 109 def write(buf) until buf.empty? # it can take more than the timeout to write out the entire buffer size = nonblocking_timeout(@write_timeout) do @socket.write_nonblock(buf) end debug "write #buf=#{buf.size}: size=#{size}" buf = buf[size..-1] end end |