我是 ruby 新手,也许这是一个非常简单的问题。我想使用 eventmachine 为我的测试开发一个模拟器。按照文档中的示例,我可以编写如下内容:
require 'eventmachine'
class Server< EM::Connection
def receive_data data
send_data data
close_connection_after_writing
end
end
#Note that this will block current thread.
EventMachine.run {
EventMachine.start_server '127.0.0.1','8080', Server
}
但我想知道是否有办法使用类的实例,例如:
require 'eventmachine'
class Server< EM::Connection
attr_accessor :response
def receive_data data
send_data @response
close_connection_after_writing
end
end
server1 = Server.new
server1.response = "foo"
#Note that this will block current thread.
EventMachine.run {
EventMachine.start_server '127.0.0.1','8080', server1
}
我尝试阅读源代码..但这对我来说太难了。我肯定错过了一些东西,但我不知道如何做这样的事情。