Class: Ferrum::Browser::WebSocket
- Inherits:
-
Object
- Object
- Ferrum::Browser::WebSocket
- Defined in:
- lib/ferrum/browser/web_socket.rb
Constant Summary collapse
- WEBSOCKET_BUG_SLEEP =
0.01
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, max_receive_size, logger) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #on_close(_event) ⇒ Object
- #on_message(event) ⇒ Object
- #on_open(_event) ⇒ Object
- #send_message(data) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(url, max_receive_size, logger) ⇒ WebSocket
Returns a new instance of WebSocket.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ferrum/browser/web_socket.rb', line 14 def initialize(url, max_receive_size, logger) @url = url @logger = logger uri = URI.parse(@url) @sock = TCPSocket.new(uri.host, uri.port) max_receive_size ||= ::WebSocket::Driver::MAX_LENGTH @driver = ::WebSocket::Driver.client(self, max_length: max_receive_size) = Queue.new @driver.on(:open, &method(:on_open)) @driver.on(:message, &method(:on_message)) @driver.on(:close, &method(:on_close)) @thread = Thread.new do Thread.current.abort_on_exception = true if Thread.current.respond_to?(:report_on_exception=) Thread.current.report_on_exception = true end begin while data = @sock.readpartial(512) @driver.parse(data) end rescue EOFError, Errno::ECONNRESET, Errno::EPIPE .close end end @driver.start end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
12 13 14 |
# File 'lib/ferrum/browser/web_socket.rb', line 12 def end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
12 13 14 |
# File 'lib/ferrum/browser/web_socket.rb', line 12 def url @url end |
Instance Method Details
#close ⇒ Object
73 74 75 |
# File 'lib/ferrum/browser/web_socket.rb', line 73 def close @driver.close end |
#on_close(_event) ⇒ Object
56 57 58 59 |
# File 'lib/ferrum/browser/web_socket.rb', line 56 def on_close(_event) .close @thread.kill end |
#on_message(event) ⇒ Object
50 51 52 53 54 |
# File 'lib/ferrum/browser/web_socket.rb', line 50 def (event) data = JSON.parse(event.data) .push(data) @logger&.puts(" ◀ #{Ferrum.elapsed_time} #{event.data}\n") end |
#on_open(_event) ⇒ Object
45 46 47 48 |
# File 'lib/ferrum/browser/web_socket.rb', line 45 def on_open(_event) # https://github.com/faye/websocket-driver-ruby/issues/46 sleep(WEBSOCKET_BUG_SLEEP) end |
#send_message(data) ⇒ Object
61 62 63 64 65 |
# File 'lib/ferrum/browser/web_socket.rb', line 61 def (data) json = data.to_json @driver.text(json) @logger&.puts("\n\n▶ #{Ferrum.elapsed_time} #{json}") end |
#write(data) ⇒ Object
67 68 69 70 71 |
# File 'lib/ferrum/browser/web_socket.rb', line 67 def write(data) @sock.write(data) rescue EOFError, Errno::ECONNRESET, Errno::EPIPE .close end |