How would I convert the example echo server into one that echos to all the other connections also? I.E. How do I get a list of all the connections from eventmachine? Thanks
There is no built in way to do this but you can simply create a variable (global if you desire) and append new connections to the array every time a new connection is created (via self)... Then you can simply loop over the connection array... I don''t have access to my code at the moment but let me know if you need me to forward it later... On 2/28/07, James Robey <jrobey at stripemobile.com> wrote:> > > How would I convert the example echo server into one that echos to all the > other connections also? I.E. How do I get a list of all the connections > from eventmachine? > > Thanks > _______________________________________________ > 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/20070228/7d5206d5/attachment.html
Bellow is the EchoServer example w/ $connections and @session. Maybe someone can show a better way than using a global for $connections? http://pastie.caboo.se/43769 -------------------------------------------------------- require ''rubygems'' require ''eventmachine '' # connections array $connections = [] module EchoServer # We have a new connection def post_init @session = {} $connections << self end # We are closing a connection def unbind $connections.delete self end def receive_data data send_data ">>>you sent: #{data}" close_connection if data =~ /quit /i end end EventMachine::run { EventMachine ::start_server "192.168.0.100", 8081, EchoServer } On 2/28/07, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> > There is no built in way to do this but you can simply create a variable > (global if you desire) and append new connections to the array every time a > new connection is created (via self)... Then you can simply loop over the > connection array... > > I don''t have access to my code at the moment but let me know if you need > me to forward it later... > > On 2/28/07, James Robey < jrobey at stripemobile.com> wrote: > > > > > > How would I convert the example echo server into one that echos to all > > the other connections also? I.E. How do I get a list of all the > > connections from eventmachine? > > > > Thanks > > _______________________________________________ > > 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/20070228/746b3d03/attachment.html
You could have EchoServer be a subclass of EventMachine::Connection rather than a module, then use a class variable On 2/28/07, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> > Bellow is the EchoServer example w/ $connections and @session. > Maybe someone can show a better way than using a global for $connections? > > > http://pastie.caboo.se/43769 > -------------------------------------------------------- > > require ''rubygems'' > require '' > eventmachine > '' > > # connections array > $connections = [] > > module EchoServer > > > # We have a new connection > def post_init > > @session = {} > $connections << self > > end > > # We are closing a connection > > def unbind > $connections.delete self > end > > > > def receive_data data > send_data ">>>you sent: > #{data}" > close_connection if data =~ /quit > /i > end > > end > > EventMachine > ::run { > EventMachine > ::start_server "192.168.0.100 > ", 8081, EchoServer > > } > > > > On 2/28/07, Daniel Aquino < mr.danielaquino at gmail.com> wrote: > > > > There is no built in way to do this but you can simply create a variable > > (global if you desire) and append new connections to the array every time a > > new connection is created (via self)... Then you can simply loop over the > > connection array... > > > > I don''t have access to my code at the moment but let me know if you need > > me to forward it later... > > > > On 2/28/07, James Robey < jrobey at stripemobile.com> wrote: > > > > > > > > > How would I convert the example echo server into one that echos to all > > > the other connections also? I.E. How do I get a list of all the > > > connections from eventmachine? > > > > > > Thanks > > > _______________________________________________ > > > Eventmachine-talk mailing list > > > Eventmachine-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >-- Tony Arcieri ClickCaster, Inc. tony at clickcaster.com (970) 232-4208 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070228/59996326/attachment.html
Here, try this instead: http://pastie.caboo.se/43785 require ''rubygems'' require ''eventmachine'' # Each new connection creates an "instance" of EchoServer class EchoServer < EventMachine::Connection @@connections = [] # We have a new connection def post_init @session = {} @@connections << self end # We are closing a connection def unbind @@connections.delete self end # We have received data def receive_data data @@connections.each {|c| c.instance_eval { send_data ">>>you sent: #{data}" } } end end EventMachine::run { EventMachine::start_server "127.0.0.1", 8081, EchoServer } Best regards, --Michael On 2/28/07, Daniel Aquino <mr.danielaquino at gmail.com> wrote:> Bellow is the EchoServer example w/ $connections and @session. > Maybe someone can show a better way than using a global for $connections? > > > http://pastie.caboo.se/43769 > -------------------------------------------------------- > require ''rubygems'' > require '' > eventmachine > '' > > # connections array > $connections = [] > > module EchoServer > > > # We have a new connection > def post_init > > @session = {} > $connections << self > > end > > # We are closing a connection > > def unbind > $connections.delete self > end > > > > def receive_data data > send_data ">>>you sent: > #{data}" > > close_connection if data =~ /quit > /i > end > > end > > EventMachine > ::run { > EventMachine > ::start_server "192.168.0.100 > ", 8081, EchoServer > > } > > > On 2/28/07, Daniel Aquino < mr.danielaquino at gmail.com> wrote: > > There is no built in way to do this but you can simply create a variable > (global if you desire) and append new connections to the array every time a > new connection is created (via self)... Then you can simply loop over the > connection array... > > > > I don''t have access to my code at the moment but let me know if you need > me to forward it later... > > > > > > > > On 2/28/07, James Robey < jrobey at stripemobile.com> wrote: > > > > > > How would I convert the example echo server into one that echos to all > the other connections also? I.E. How do I get a list of all the > connections from eventmachine? > > > > > > Thanks > > > _______________________________________________ > > > Eventmachine-talk mailing list > > > Eventmachine-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > > > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
On 2/28/07, James Robey <jrobey at stripemobile.com> wrote:> > > How would I convert the example echo server into one that echos to all the > other connections also? I.E. How do I get a list of all the connections > from eventmachine? > > Thanks > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >Sounds like someone is building a multicasting chat server. Yes? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/eventmachine-talk/attachments/20070228/b6ebaec0/attachment.html
Thanks for the ideas guys. On Wed, 28 Feb 2007 15:58:26 -0800 "Michael S. Fischer" <michael at dynamine.net> wrote:> Here, try this instead: > > http://pastie.caboo.se/43785 > > require ''rubygems'' > require ''eventmachine'' > > # Each new connection creates an "instance" of EchoServer > class EchoServer < EventMachine::Connection > > @@connections = [] > > # We have a new connection > def post_init > @session = {} > @@connections << self > end > > # We are closing a connection > def unbind > @@connections.delete self > end > > # We have received data > def receive_data data > @@connections.each {|c| > c.instance_eval { send_data ">>>you sent: #{data}" } > } > end > > end > > EventMachine::run { > EventMachine::start_server "127.0.0.1", 8081, EchoServer > } > > Best regards, > > --Michael > > On 2/28/07, Daniel Aquino <mr.danielaquino at gmail.com> wrote: > > Bellow is the EchoServer example w/ $connections and @session. > > Maybe someone can show a better way than using a global for $connections? > > > > > > http://pastie.caboo.se/43769 > > -------------------------------------------------------- > > require ''rubygems'' > > require '' > > eventmachine > > '' > > > > # connections array > > $connections = [] > > > > module EchoServer > > > > > > # We have a new connection > > def post_init > > > > @session = {} > > $connections << self > > > > end > > > > # We are closing a connection > > > > def unbind > > $connections.delete self > > end > > > > > > > > def receive_data data > > send_data ">>>you sent: > > #{data}" > > > > close_connection if data =~ /quit > > /i > > end > > > > end > > > > EventMachine > > ::run { > > EventMachine > > ::start_server "192.168.0.100 > > ", 8081, EchoServer > > > > } > > > > > > On 2/28/07, Daniel Aquino < mr.danielaquino at gmail.com> wrote: > > > There is no built in way to do this but you can simply create a variable > > (global if you desire) and append new connections to the array every time a > > new connection is created (via self)... Then you can simply loop over the > > connection array... > > > > > > I don''t have access to my code at the moment but let me know if you need > > me to forward it later... > > > > > > > > > > > > On 2/28/07, James Robey < jrobey at stripemobile.com> wrote: > > > > > > > > How would I convert the example echo server into one that echos to all > > the other connections also? I.E. How do I get a list of all the > > connections from eventmachine? > > > > > > > > Thanks > > > > _______________________________________________ > > > > Eventmachine-talk mailing list > > > > Eventmachine-talk at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > > > > > > > > > > > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > _______________________________________________ > Eventmachine-talk mailing list > Eventmachine-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/eventmachine-talk >
Actually I was just curious about how to maintain data across instances and I was unsure how to do that. Still learning ruby :) Plus I did a ''multi-user'' echo server in forth, thought I might as well do one in ruby ;) Why, do you already have open source of a multicasting chat server? On Wed, 28 Feb 2007 23:49:34 -0500 "Francis Cianfrocca" <garbagecat10 at gmail.com> wrote:> On 2/28/07, James Robey <jrobey at stripemobile.com> wrote: > > > > > > How would I convert the example echo server into one that echos to all the > > other connections also? I.E. How do I get a list of all the connections > > from eventmachine? > > > > Thanks > > _______________________________________________ > > Eventmachine-talk mailing list > > Eventmachine-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/eventmachine-talk > > > > > Sounds like someone is building a multicasting chat server. Yes? >