Module: Ronin::Support::Web::WebSocket
- Defined in:
- lib/ronin/support/web/websocket.rb,
lib/ronin/support/web/websocket/mixin.rb,
lib/ronin/support/web/websocket/client.rb,
lib/ronin/support/web/websocket/server.rb,
lib/ronin/support/web/websocket/socket.rb,
lib/ronin/support/web/websocket/url_methods.rb
Overview
WebSocket helper methods.
Defined Under Namespace
Modules: Mixin, URLMethods Classes: Client, Server, Socket
Class Method Summary collapse
-
.accept(url, ssl: {}, **kwargs) {|client| ... } ⇒ nil
Opens a WebSocket server, accepts a single connection, yields it to the given block, then closes both the connection and the server.
-
.connect(url, ssl: {}, **kwargs) {|websocket| ... } ⇒ Client
Connects to a websocket.
-
.connect_and_send(data, url, type: :text, ssl: {}, **kwargs) {|websocket| ... } ⇒ Client
Connects to the WebSocket and sends the data.
-
.open?(url, ssl: {}, **kwargs) ⇒ Boolean?
Tests whether the WebSocket is open.
-
.send(data, url, type: :text, ssl: {}, **kwargs) ⇒ true
Connects to the WebSocket, sends the data, and closes the connection.
-
.server(url, ssl: {}, **kwargs) {|server| ... } ⇒ Server
Starts a WebSocket server.
-
.server_loop(url, ssl: {}, **kwargs) {|client| ... } ⇒ nil
Creates a new WebSocket server listening on a given host and port, accepting clients in a loop.
Class Method Details
.accept(url, ssl: {}, **kwargs) {|client| ... } ⇒ nil
Opens a WebSocket server, accepts a single connection, yields it to the given block, then closes both the connection and the server.
349 350 351 352 353 354 355 356 |
# File 'lib/ronin/support/web/websocket.rb', line 349 def self.accept(url, ssl: {}, **kwargs) server(url, ssl: ssl, **kwargs) do |server| client = server.accept yield client if block_given? client.close end end |
.connect(url, ssl: {}, **kwargs) {|websocket| ... } ⇒ Client
Connects to a websocket.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ronin/support/web/websocket.rb', line 145 def self.connect(url, ssl: {}, **kwargs) client = Client.new(url, ssl: ssl, **kwargs) if block_given? yield client client.close else client end end |
.connect_and_send(data, url, type: :text, ssl: {}, **kwargs) {|websocket| ... } ⇒ Client
Connects to the WebSocket and sends the data.
190 191 192 193 194 195 196 |
# File 'lib/ronin/support/web/websocket.rb', line 190 def self.connect_and_send(data,url, type: :text, ssl: {}, **kwargs) client = connect(url, ssl: ssl, **kwargs) client.send(data, type: type) yield client if block_given? return client end |
.open?(url, ssl: {}, **kwargs) ⇒ Boolean?
Tests whether the WebSocket is open.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ronin/support/web/websocket.rb', line 94 def self.open?(url, ssl: {}, **kwargs) uri = URI(url) host = uri.host port = uri.port case uri.scheme when 'ws' Support::Network::TCP.open?(host,port,**kwargs) when 'wss' Support::Network::SSL.open?(host,port,**kwargs,**ssl) else raise(ArgumentError,"unsupported WebSocket URI scheme: #{url.inspect}") end end |
.send(data, url, type: :text, ssl: {}, **kwargs) ⇒ true
Connects to the WebSocket, sends the data, and closes the connection.
223 224 225 226 227 228 229 |
# File 'lib/ronin/support/web/websocket.rb', line 223 def self.send(data,url, type: :text, ssl: {}, **kwargs) connect(url, ssl: ssl, **kwargs) do |client| client.send(data, type: type) end return true end |
.server(url, ssl: {}, **kwargs) {|server| ... } ⇒ Server
Starts a WebSocket server.
271 272 273 274 275 276 277 278 279 280 |
# File 'lib/ronin/support/web/websocket.rb', line 271 def self.server(url, ssl: {}, **kwargs) server = Server.new(url, ssl: ssl, **kwargs) if block_given? yield server server.close else server end end |
.server_loop(url, ssl: {}, **kwargs) {|client| ... } ⇒ nil
Creates a new WebSocket server listening on a given host and port, accepting clients in a loop.
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/ronin/support/web/websocket.rb', line 310 def self.server_loop(url, ssl: {}, **kwargs) server(url, ssl: ssl, **kwargs) do |server| loop do client = server.accept yield client if block_given? client.close end end end |