On 10/17/06, Justin Collins <collinsj / seattleu.edu> wrote: > > You may also want to look at GServer[1] in the standard library, which > is probably simpler to use. Putting this together with the Observer[2] > class, it makes it fairly simple, as long as you are okay with handling > threads and such. I'd recommend using YAML[3] for storing your users and > their settings/passwords/etc. > > For the basic idea, you might start off with something like this (simply > echoes input to everyone): > > require 'gserver' > require 'observer' > > class MyServer < GServer > include Observable > > def initialize(port = 5555, *args) > super(port, *args) > end > > def serve(io) > user = User.new(io) > self.add_observer(user) > user.add_observer(self) > user.run > end > > def update(message) > changed > notify_observers(message) > end > end > > class User > include Observable > > def initialize(io) > @io = io > end > > def update(message) > @io.puts message > end > > def run > loop do > input = @io.gets > changed > notify_observers(input) > end > end > end > > MyServer.new.start.join > > > Just run that, and connect a couple of telnet sessions to port 5555. > Type in a message and it should be echoed to everyone. Hooray! > Note that MyServer and User are both observing each other, so that the > server can echo a message from any user to all the others (including the > original sender). This simplifies having to worry about threads and > queues and such. > I hope that will give you some ideas. > > -Justin > > [1]http://ruby-doc.org/stdlib/libdoc/gserver/rdoc/index.html > [2]http://ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html > [3]http://yaml4r.sourceforge.net/doc/ > > > > You may also want to look into eventmachine, a really nice event driven framework for developing network applications. http://rubyforge.org/projects/eventmachine -- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary.