Sha256: 81b09ebe61707a408eb6b1cc41e09ce8b9ef3cb2a341ce707cd50122933d50fc

Contents?: true

Size: 964 Bytes

Versions: 9

Compression:

Stored size: 964 Bytes

Contents

require 'em-websocket-client'

module Goliath
  module TestHelper
    class WSHelper
      attr_reader :connection
      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

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

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

    def ws_client_connect(path,&blk)
      url = "ws://localhost:#{@test_server_port}#{path}"
      client = WSHelper.new(url)
      blk.call(client) if blk
      stop
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
goliath-1.0.7 lib/goliath/test_helper_ws.rb
goliath-1.0.6 lib/goliath/test_helper_ws.rb
goliath-1.0.5 lib/goliath/test_helper_ws.rb
goliath-1.0.4 lib/goliath/test_helper_ws.rb
goliath-1.0.3 lib/goliath/test_helper_ws.rb
goliath-1.0.2 lib/goliath/test_helper_ws.rb
goliath-1.0.1 lib/goliath/test_helper_ws.rb
goliath-1.0.0 lib/goliath/test_helper_ws.rb
goliath-1.0.0.beta.1 lib/goliath/test_helper_ws.rb