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).
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.
258 259 260 |
# File 'cybertrain/websocket/client.rb', line 258 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).
196 197 198 |
# File 'cybertrain/websocket/client.rb', line 196 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.
246 247 248 249 250 251 252 |
# File 'cybertrain/websocket/client.rb', line 246 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.
220 221 222 |
# File 'cybertrain/websocket/client.rb', line 220 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.
228 229 230 231 232 233 |
# File 'cybertrain/websocket/client.rb', line 228 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.
212 213 214 |
# File 'cybertrain/websocket/client.rb', line 212 def send_text(text) ws_send(Frame::OP_TEXT, text.to_s) end |
#ws_label ⇒ String
“host:port”, what error messages name.
203 204 205 |
# File 'cybertrain/websocket/client.rb', line 203 def ws_label @label end |
#ws_open? ⇒ Boolean
Returns false once the connection is closed (by #close, by the server or by an error).
188 189 190 |
# File 'cybertrain/websocket/client.rb', line 188 def ws_open? @open end |