File: server.rb

package info (click to toggle)
ruby-poltergeist 1.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 368 kB
  • sloc: ruby: 1,528; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
46
47
48
# frozen_string_literal: true

module Capybara::Poltergeist
  class Server
    attr_reader :socket, :fixed_port, :timeout, :custom_host

    def initialize(fixed_port = nil, timeout = nil, custom_host = nil)
      @fixed_port = fixed_port
      @timeout    = timeout
      @custom_host = custom_host
      start
    end

    def port
      @socket.port
    end

    def host
      @socket.host
    end

    def timeout=(sec)
      @timeout = @socket.timeout = sec
    end

    def start
      @socket = WebSocketServer.new(fixed_port, timeout, custom_host)
    end

    def stop
      @socket.close
    end

    def restart
      stop
      start
    end

    def send(command)
      receive_timeout = nil # default
      if command.name == 'visit'
        command.args.push(timeout) # set the client set visit timeout parameter
        receive_timeout = timeout + 5 # Add a couple of seconds to let the client timeout first
      end
      @socket.send(command.id, command.message, receive_timeout) or raise DeadClient.new(command.message)
    end
  end
end