Sha256: b9f09503d0cddea8c53a783693bca23c3e0ab6c817218a4aa04be58ab4ca8859

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

require 'bundler/setup'
require 'eventmachine'

def answer(data)
  case data
    when 'ping' then "pong\n"
    when 'bad' then "what\n"
    when 'timeout' then sleep 5; "ok\n"
    when 'exception' then raise 'haha'
    when 'quit' then EM.stop
    when 'big' then 'a' * 10_000_000    
  end
end

class Echo < EM::Connection
  def post_init
    puts "-- someone connected to the echo server!"
  end

  def receive_data data
    puts "receive #{data.inspect}"
    send_data(answer(data))
  end

  def unbind
     puts "-- someone disconnected from the echo server!"
  end  
end

class EchoObj < EM::Connection
  include EM::P::ObjectProtocol

  def post_init
    puts "-- someone connected to the echo server!"
  end

  def receive_object obj # {:command => 'ping'}
    puts "receive #{obj.inspect}"
    send_object(answer(obj[:command]).chop)
  end

  def unbind
    puts "-- someone disconnected from the echo server!"
  end  
end

trap "TERM" do
  EM.stop
  `rm /tmp/em_test_sock_spec` rescue nil
end

EM.run do
  EM.start_server '127.0.0.1', 33231, Echo
  EM.start_server '127.0.0.1', 33232, EchoObj
  EM.start_server "/tmp/em_test_sock_spec", nil, Echo
  puts 'started'
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
eye-0.2.4 spec/example/em.rb
eye-0.2.3 spec/example/em.rb
eye-0.2.2 spec/example/em.rb
eye-0.2.1 spec/example/em.rb
eye-0.2 spec/example/em.rb
eye-0.1.11 spec/example/em.rb