Class: VcrWs::ActorWS

Inherits:
Object
  • Object
show all
Defined in:
lib/vcr_ws/actor_ws.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ ActorWS

Returns a new instance of ActorWS.



3
4
5
6
# File 'lib/vcr_ws/actor_ws.rb', line 3

def initialize(file_path)
  @events = load_events(file_path)
  @event_index = 0
end

Instance Method Details

#start!Object



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
# File 'lib/vcr_ws/actor_ws.rb', line 8

def start!
  cnf = VcrWs::Config.instance
  @thread = Thread.new(cnf.test_ws_address, cnf.test_ws_port) do |host, port|
    Thread.handle_interrupt(RuntimeError => :never) do
      # You can write resource allocation code safely.
      Thread.handle_interrupt(RuntimeError => :immediate) do
        EM.run do
          puts "Starting VCR WebSocket server on ws://#{host}:#{port}"
          EM::WebSocket.start(host:, port:) do |ws|
            ws.onopen do
              handle_open(ws)
            end

            ws.onmessage do |message|
              receive_message(ws, message)
            end

            ws.onclose do
              handle_close(ws)
            end

            ws.onerror do |error|
              puts "VCR::Error: #{error.message}"
              puts error.backtrace
            end
          end
        end
      end
    ensure
      EM.stop if EM.reactor_running?
    end
  end.run
  sleep 3
end

#stop!Object



43
44
45
# File 'lib/vcr_ws/actor_ws.rb', line 43

def stop!
  EM.stop if EM.reactor_running?
end