Francis Cianfrocca
2006-May-24 10:33 UTC
[Eventmachine-talk] Storing connections in your program (reply to
The EventMachine echo-server shows a module that you supply to handle
events. But you can also use a class. If you do so, then you have more
flexibility to add other functionality to the connection objects that
EventMachine generates when it accepts network connections.
For example, if you wanted an array of currently-open connections, try this:
require ''rubygems''
require ''eventmachine''
class Echo < EventMachine::Connection
@@conn_list = []
def self.print_connections
p @@conn_list
end
def initialize *args
super
@@conn_list << self
end
def post_init
# your normal processing
end
def receive_data data
# your normal processing
end
def unbind
@@conn_list.delete self
end
end
EventMachine.run {
EventMachine.start_server "192.168.xx.yy", 8100, Echo
EventMachine.add_periodic_timer(5) {Echo.print_connections}
}