Method: EventMachine.connect

Defined in:
lib/eventmachine.rb

.connect(server, port = nil, handler = nil, *args, &blk) ⇒ Object

Initiates a TCP connection to a remote server and sets up event handling for the connection. connect requires event loop to be running (see run).

connect takes the IP address (or hostname) and port of the remote server you want to connect to. It also takes an optional handler (a module or a subclass of Connection) which you must define, that contains the callbacks that will be invoked by the event loop on behalf of the connection.

Learn more about connection lifecycle callbacks in the EventMachine tutorial and Connection lifecycle guide.

Examples:


# Here's a program which connects to a web server, sends a naive
# request, parses the HTTP header of the response, and then
# (antisocially) ends the event loop, which automatically drops the connection
# (and incidentally calls the connection's unbind method).
module DumbHttpClient
  def post_init
    send_data "GET / HTTP/1.1\r\nHost: _\r\n\r\n"
    @data = ""
    @parsed = false
  end

  def receive_data data
    @data << data
    if !@parsed and @data =~ /[\n][\r]*[\n]/m
      @parsed = true
      puts "RECEIVED HTTP HEADER:"
      $`.each {|line| puts ">>> #{line}" }

      puts "Now we'll terminate the loop, which will also close the connection"
      EventMachine::stop_event_loop
    end
  end

  def unbind
    puts "A connection has terminated"
  end
end

EventMachine.run {
  EventMachine.connect "www.bayshorenetworks.com", 80, DumbHttpClient
}
puts "The event loop has ended"

Defining protocol handler as a class


class MyProtocolHandler < EventMachine::Connection
  def initialize *args
    super
    # whatever else you want to do here
  end

  # ...
end

Parameters:

  • server (String)

    Host to connect to

  • port (Integer) (defaults to: nil)

    Port to connect to

  • handler (Module, Class) (defaults to: nil)

    A module or class that implements connection lifecycle callbacks

See Also:



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/eventmachine.rb', line 631

def self.connect server, port=nil, handler=nil, *args, &blk
  # EventMachine::connect initiates a TCP connection to a remote
  # server and sets up event-handling for the connection.
  # It internally creates an object that should not be handled
  # by the caller. HOWEVER, it's often convenient to get the
  # object to set up interfacing to other objects in the system.
  # We return the newly-created anonymous-class object to the caller.
  # It's expected that a considerable amount of code will depend
  # on this behavior, so don't change it.
  #
  # Ok, added support for a user-defined block, 13Apr06.
  # This leads us to an interesting choice because of the
  # presence of the post_init call, which happens in the
  # initialize method of the new object. We call the user's
  # block and pass the new object to it. This is a great
  # way to do protocol-specific initiation. It happens
  # AFTER post_init has been called on the object, which I
  # certainly hope is the right choice.
  # Don't change this lightly, because accepted connections
  # are different from connected ones and we don't want
  # to have them behave differently with respect to post_init
  # if at all possible.

  bind_connect nil, nil, server, port, handler, *args, &blk
end