Class: Capybara::Poltergeist::WebSocketServer
- Inherits:
-
Object
- Object
- Capybara::Poltergeist::WebSocketServer
- Defined in:
- lib/capybara/poltergeist/web_socket_server.rb
Overview
This is a ‘custom’ Web Socket server that is designed to be synchronous. What this means is that it sends a message, and then waits for a response. It does not expect to receive a message at any other time than right after it has sent a message. So it is basically operating a request/response cycle (which is not how Web Sockets are usually used, but it’s what we want here, as we want to send a message to PhantomJS and then wait for it to respond).
Constant Summary collapse
- RECV_SIZE =
How much to try to read from the socket at once (it’s kinda arbitrary because we just keep reading until we’ve received a full frame)
1024
- BIND_TIMEOUT =
How many seconds to try to bind to the port for before failing
5
- HOST =
'127.0.0.1'
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#accept ⇒ Object
Accept a client on the TCP server socket, then receive its initial HTTP request and use that to initialize a Web Socket.
-
#close ⇒ Object
Closing sockets separately as ‘close_read`, `close_write` causes IO mistakes on JRuby, using just `close` fixes that.
- #connected? ⇒ Boolean
-
#initialize(port = nil, timeout = nil) ⇒ WebSocketServer
constructor
A new instance of WebSocketServer.
-
#receive(cmd_id) ⇒ Object
Block until the next message is available from the Web Socket.
-
#send(cmd_id, message) ⇒ Object
Send a message and block until there is a response.
- #start_server(port) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(port = nil, timeout = nil) ⇒ WebSocketServer
Returns a new instance of WebSocketServer.
24 25 26 27 28 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 24 def initialize(port = nil, timeout = nil) @timeout = timeout @server = start_server(port) @receive_mutex = Mutex.new end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
21 22 23 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 21 def driver @driver end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
21 22 23 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 21 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
21 22 23 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 21 def server @server end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
21 22 23 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 21 def socket @socket end |
#timeout ⇒ Object
Returns the value of attribute timeout.
22 23 24 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 22 def timeout @timeout end |
Instance Method Details
#accept ⇒ Object
Accept a client on the TCP server socket, then receive its initial HTTP request and use that to initialize a Web Socket.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 53 def accept @socket = server.accept @messages = {} @driver = ::WebSocket::Driver.server(self) @driver.on(:connect) { |event| @driver.start } @driver.on(:message) do |event| command_id = JSON.load(event.data)['command_id'] @messages[command_id] = event.data end end |
#close ⇒ Object
Closing sockets separately as ‘close_read`, `close_write` causes IO mistakes on JRuby, using just `close` fixes that.
103 104 105 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 103 def close [server, socket].compact.each(&:close) end |
#connected? ⇒ Boolean
47 48 49 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 47 def connected? !socket.nil? end |
#receive(cmd_id) ⇒ Object
Block until the next message is available from the Web Socket. Raises Errno::EWOULDBLOCK if timeout is reached.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 71 def receive(cmd_id) start = Time.now until @messages.has_key?(cmd_id) raise Errno::EWOULDBLOCK if (Time.now - start) >= timeout if @receive_mutex.try_lock begin IO.select([socket], [], [], timeout) or raise Errno::EWOULDBLOCK data = socket.recv(RECV_SIZE) break if data.empty? driver.parse(data) ensure @receive_mutex.unlock end else sleep(0.05) end end @messages.delete(cmd_id) end |
#send(cmd_id, message) ⇒ Object
Send a message and block until there is a response
93 94 95 96 97 98 99 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 93 def send(cmd_id, ) accept unless connected? driver.text() receive(cmd_id) rescue Errno::EWOULDBLOCK raise TimeoutError.new() end |
#start_server(port) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 30 def start_server(port) time = Time.now begin TCPServer.open(HOST, port || 0).tap do |server| @port = server.addr[1] end rescue Errno::EADDRINUSE if (Time.now - time) < BIND_TIMEOUT sleep(0.01) retry else raise end end end |
#write(data) ⇒ Object
65 66 67 |
# File 'lib/capybara/poltergeist/web_socket_server.rb', line 65 def write(data) @socket.write(data) end |