Hello, the problem is that I don''t want to store the connections in a self- containing way like Francis did in his code example. I am working on a game server where users are represented as objects existing in and moving between rooms to group them. There should be no general difference between remote users and bots. So I''d like to store the remote user''s connections directly into them so they can use it individually for their communication purposes. At least in this case that seems to me more elegant than storing them all within the Connection class. Here is an idea to achieve this goal: require ''rubygems'' require ''eventmachine'' module EventMachine class Connection @@ userManager = false def self.userManager= userManager @@userManager = userManager end def accept connection puts @@userManager, connection #@@userManager.accept connection if @@userManager end end end module EchoServer def post_init accept self end def receive_data data send_data ">>>you sent: #{data}" if data =~ /quit/i close_connection end if data =~ /shutdown/i close_connection EventMachine::stop_event_loop end end end EventMachine::run { EventMachine::Connection.userManager = "any class or object with an accept(connection) method" EventMachine::start_server "127.0.0.1", 8081, EchoServer } The manager is an object tied to your application which accepts the new connection and creates a new remote user object. So the EventMachine module is clearly separated from the application code. So I guess this code should solve my problem. Maybe it is helpful for other programmers , too. If the additional code snippets from the Connection class are implemented everyone can decide to use this feature by setting a userManager und calling accept within post_init. What do you think about it? Regards, Sascha
Francis Cianfrocca
2006-May-24 14:29 UTC
[Eventmachine-talk] an idea to improve eventmachine
If you need to store the Connection objects in some completely separate facility, then why not call an external method in post_init? In the following example, there is a separate class, GameClients, with a class method that accepts the new connection. module GameClientConnection def post_init GameClients.accept this end end I''m still not seeing how it would be better to store a reference to this external object inside the EventMachine::Connection class. On 5/24/06, Sascha Ernst <mail at big-ernesto.de> wrote:> > Hello, > > the problem is that I don''t want to store the connections in a self- > containing way like Francis did in his code example. I am working on > a game server where users are represented as objects existing in and > moving between rooms to group them. There should be no general > difference between remote users and bots. So I''d like to store the > remote user''s connections directly into them so they can use it > individually for their communication purposes. At least in this case > that seems to me more elegant than storing them all within the > Connection class. > > Here is an idea to achieve this goal: > > require ''rubygems'' > require ''eventmachine'' > > module EventMachine > class Connection > @@ userManager = false > > def self.userManager= userManager > @@userManager = userManager > end > > def accept connection > puts @@userManager, connection > #@@userManager.accept connection if @@userManager > end > end > end > > > module EchoServer > def post_init > accept self > end > > def receive_data data > send_data ">>>you sent: #{data}" > > if data =~ /quit/i > close_connection > end > > if data =~ /shutdown/i > close_connection > EventMachine::stop_event_loop > end > end > end > > EventMachine::run { > EventMachine::Connection.userManager = "any class or object with > an > accept(connection) method" > EventMachine::start_server "127.0.0.1", 8081, EchoServer > } > > > > The manager is an object tied to your application which accepts the > new connection and creates a new remote user object. So the > EventMachine module is clearly separated from the application code. > > So I guess this code should solve my problem. Maybe it is helpful for > other programmers , too. If the additional code snippets from the > Connection class are implemented everyone can decide to use this > feature by setting a userManager und calling accept within post_init. > > What do you think about it? > > > Regards, Sascha > > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20060524/e9cf024d/attachment-0001.htm