mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00

Ask, don't tell, or something like that! Now the WebSocketServer decides what port it will use and everything else relies on that. By default an ephemeral port is used, but a fixed port number can be passed in too. This work is necessary to support Windows (#240) because on Windows a TCP socket cannot be closed and immediately reopened, which we are doing in the tests. So this change means that when restarting, a new ephemeral port can be used by the server.
36 lines
600 B
Ruby
36 lines
600 B
Ruby
module Capybara::Poltergeist
|
|
class Server
|
|
attr_reader :socket, :fixed_port, :timeout
|
|
|
|
def initialize(port = nil, timeout = nil)
|
|
@fixed_port = fixed_port
|
|
@timeout = timeout
|
|
start
|
|
end
|
|
|
|
def port
|
|
@socket.port
|
|
end
|
|
|
|
def timeout=(sec)
|
|
@timeout = @socket.timeout = sec
|
|
end
|
|
|
|
def start
|
|
@socket = WebSocketServer.new(fixed_port, timeout)
|
|
end
|
|
|
|
def stop
|
|
@socket.close
|
|
end
|
|
|
|
def restart
|
|
stop
|
|
start
|
|
end
|
|
|
|
def send(message)
|
|
@socket.send(message) or raise DeadClient.new(message)
|
|
end
|
|
end
|
|
end
|