Class: Goliath::TestHelper::WSHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/goliath/test_helper_ws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ WSHelper

Returns a new instance of WSHelper.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/goliath/test_helper_ws.rb', line 7

def initialize(url)
  @queue = EM::Queue.new

  fiber = Fiber.current
  @connection = EventMachine::WebSocketClient.connect(url)
  @connection.errback do |e|
    puts "Error encountered during connection: #{e}"
    EM::stop_event_loop
  end

  @connection.callback { fiber.resume }
  @connection.disconnect { EM::stop_event_loop }
  @connection.stream { |m| @queue.push(m) }

  Fiber.yield
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/goliath/test_helper_ws.rb', line 6

def connection
  @connection
end

Instance Method Details

#receiveObject



28
29
30
31
32
# File 'lib/goliath/test_helper_ws.rb', line 28

def receive
  fiber = Fiber.current
  @queue.pop {|m| fiber.resume(m) }
  Fiber.yield
end

#send(m) ⇒ Object



24
25
26
# File 'lib/goliath/test_helper_ws.rb', line 24

def send(m)
  @connection.send_msg(m)
end