Class: Cybertrain::WebSocket::Client
- Inherits:
-
Object
- Object
- Cybertrain::WebSocket::Client
- Defined in:
- cybertrain/websocket/client.rb
Overview
A WebSocket connection to a server, used from one thread.
Class Method Summary collapse
-
.connect(url, headers: NO_HEADERS, timeout: 5, max_message: 1_048_576, allow_private: Cybertrain.config.http_allow_private) ⇒ Client
Connects and performs the opening handshake.
Instance Method Summary collapse
-
#close ⇒ nil
Sends close 1000, waits at most
timeoutfor the server’s close and closes the connection. -
#last_activity_at ⇒ Float
When a frame last arrived (or the connection opened), on the monotonic clock (Net::Timer.now).
-
#receive(wait) ⇒ Message?
The next data message, waiting at most
waitseconds. -
#send_binary(data) ⇒ nil
Sends a binary message in one frame.
-
#send_ping(payload = "") ⇒ nil
Sends a ping; the server’s pong counts as activity.
-
#send_text(text) ⇒ nil
Sends a text message in one frame.
-
#ws_label ⇒ String
“host:port”, what error messages name.
-
#ws_open? ⇒ Boolean
False once the connection is closed (by #close, by the server or by an error).
-
#ws_pongs ⇒ Integer
How many pongs (answers to #send_ping) have arrived.
Class Method Details
.connect(url, headers: NO_HEADERS, timeout: 5, max_message: 1_048_576, allow_private: Cybertrain.config.http_allow_private) ⇒ Client
Connects and performs the opening handshake.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'cybertrain/websocket/client.rb', line 63 def self.connect(url, headers: NO_HEADERS, timeout: 5, max_message: 1_048_576, allow_private: Cybertrain.config.http_allow_private) target = HttpClient.parse_url_for(url, "ws://", "wss://") seconds = timeout.to_f limit = .to_i raise ArgumentError, "timeout must be positive" unless seconds > 0.0 raise ArgumentError, "max_message must be at least 1" if limit < 1 key = Base64.strict_encode64(SecureRandom.random_bytes(16)) head = Client.handshake_head(target, key, headers) Client.ws_open(target, key, head, seconds, limit, allow_private == true) end |
Instance Method Details
#close ⇒ nil
Sends close 1000, waits at most timeout for the server’s close and
closes the connection. Safe to call twice; never raises.
267 268 269 |
# File 'cybertrain/websocket/client.rb', line 267 def close ws_close_within(@timeout) end |
#last_activity_at ⇒ Float
When a frame last arrived (or the connection opened), on the monotonic clock (Net::Timer.now).
198 199 200 |
# File 'cybertrain/websocket/client.rb', line 198 def last_activity_at @last_activity_at end |
#receive(wait) ⇒ Message?
The next data message, waiting at most wait seconds. Pings are
answered and pongs noted on the way; once wait has passed it
answers nil after the next control frame or fragment too, so a
server that streams pings or pongs cannot keep it looping.
255 256 257 258 259 260 261 |
# File 'cybertrain/websocket/client.rb', line 255 def receive(wait) ws_require_open seconds = wait.to_f @receive_ends_at = Net::Timer.now + seconds @reader.reader_timer = Net::Timer.new(@label, seconds, seconds) ws_receive_guarded end |
#send_binary(data) ⇒ nil
Sends a binary message in one frame.
229 230 231 |
# File 'cybertrain/websocket/client.rb', line 229 def send_binary(data) ws_send(Frame::OP_BINARY, data.to_s) end |
#send_ping(payload = "") ⇒ nil
Sends a ping; the server’s pong counts as activity.
237 238 239 240 241 242 |
# File 'cybertrain/websocket/client.rb', line 237 def send_ping(payload = "") data = payload.to_s raise ArgumentError, "a ping payload is at most 125 bytes" if data.bytesize > 125 ws_send(Frame::OP_PING, data) end |
#send_text(text) ⇒ nil
Sends a text message in one frame.
221 222 223 |
# File 'cybertrain/websocket/client.rb', line 221 def send_text(text) ws_send(Frame::OP_TEXT, text.to_s) end |
#ws_label ⇒ String
“host:port”, what error messages name.
212 213 214 |
# File 'cybertrain/websocket/client.rb', line 212 def ws_label @label end |
#ws_open? ⇒ Boolean
Returns false once the connection is closed (by #close, by the server or by an error).
190 191 192 |
# File 'cybertrain/websocket/client.rb', line 190 def ws_open? @open end |
#ws_pongs ⇒ Integer
How many pongs (answers to #send_ping) have arrived.
205 206 207 |
# File 'cybertrain/websocket/client.rb', line 205 def ws_pongs @pongs end |