A basic chat program

Discussion in 'Ruby' started by Michael Hall, Oct 16, 2006.

  1. Michael Hall

    Michael Hall Guest

    Okay I'm looking to make a basic chat program that I can connect to via
    telnet for learning purposes.

    I want a user to connect then login in or be able to create a new user,
    new users would be saved as files. then when they're in the program they
    can talk on different channels by typing "channel1 <message>" or
    "channel2 <message>" etc. Also I'd like to put the code in different
    files i.e. connection/login/user create code in one file, the
    channel/talk functions in another and in a third file a list of the
    commands that can be typed pointing to the function name (like
    {"<command", function_name} so that when they type the command that's
    listed there it will find the correct function and run it) I need help
    on how to go about this, psuedo code, code examples etc.

    --
    Posted via http://www.ruby-forum.com/.
     
    Michael Hall, Oct 16, 2006
    #1
    1. Advertising

  2. Michael Hall

    Jan Svitok Guest

    On 10/17/06, Michael Hall <> wrote:
    > Okay I'm looking to make a basic chat program that I can connect to via
    > telnet for learning purposes.
    >
    > I want a user to connect then login in or be able to create a new user,
    > new users would be saved as files. then when they're in the program they
    > can talk on different channels by typing "channel1 <message>" or
    > "channel2 <message>" etc. Also I'd like to put the code in different
    > files i.e. connection/login/user create code in one file, the
    > channel/talk functions in another and in a third file a list of the
    > commands that can be typed pointing to the function name (like
    > {"<command", function_name} so that when they type the command that's
    > listed there it will find the correct function and run it) I need help
    > on how to go about this, psuedo code, code examples etc.


    Have a look at Socket::TCPServer in stdlib. That would be the base for
    your server.
     
    Jan Svitok, Oct 16, 2006
    #2
    1. Advertising

  3. Michael Hall

    Guest

    Michael,

    Jon Lambert is developing a mud server which should have most of the
    functionality and architecture you're looking for, it would be a good
    example to see how things are put together.

    Unfortunately Lambert's servers are offline and I don't know when
    they're coming back on; however there is an archive of the 07-07-2006
    2.10 release at this site:

    http://www.mudmagic.com/codes/server-snippet/2437


    best, George


    Michael Hall wrote:
    > Okay I'm looking to make a basic chat program that I can connect to via
    > telnet for learning purposes.
    >
    > I want a user to connect then login in or be able to create a new user,
    > new users would be saved as files. then when they're in the program they
    > can talk on different channels by typing "channel1 <message>" or
    > "channel2 <message>" etc. Also I'd like to put the code in different
    > files i.e. connection/login/user create code in one file, the
    > channel/talk functions in another and in a third file a list of the
    > commands that can be typed pointing to the function name (like
    > {"<command", function_name} so that when they type the command that's
    > listed there it will find the correct function and run it) I need help
    > on how to go about this, psuedo code, code examples etc.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
     
    , Oct 17, 2006
    #3
  4. Jan Svitok wrote:
    > On 10/17/06, Michael Hall <> wrote:
    >> Okay I'm looking to make a basic chat program that I can connect to via
    >> telnet for learning purposes.
    >>
    >> I want a user to connect then login in or be able to create a new user,
    >> new users would be saved as files. then when they're in the program they
    >> can talk on different channels by typing "channel1 <message>" or
    >> "channel2 <message>" etc. Also I'd like to put the code in different
    >> files i.e. connection/login/user create code in one file, the
    >> channel/talk functions in another and in a third file a list of the
    >> commands that can be typed pointing to the function name (like
    >> {"<command", function_name} so that when they type the command that's
    >> listed there it will find the correct function and run it) I need help
    >> on how to go about this, psuedo code, code examples etc.

    >
    > Have a look at Socket::TCPServer in stdlib. That would be the base for
    > your server.
    >


    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/
     
    Justin Collins, Oct 17, 2006
    #4
  5. Michael Hall

    hemant Guest

    On 10/17/06, Justin Collins <> 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.
     
    hemant, Oct 17, 2006
    #5
  6. On Oct 16, 2006, at 5:24 PM, Michael Hall wrote:

    > Okay I'm looking to make a basic chat program that I can connect to
    > via
    > telnet for learning purposes.


    Page 533 of the Ruby Cookbook has a slim but functional chat server
    in about 30 lines of Ruby code.

    James Edward Gray II
     
    James Edward Gray II, Nov 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Cooke [MSFT]
    Replies:
    0
    Views:
    361
    Jason Cooke [MSFT]
    Oct 3, 2003
  2. Jason Cooke [MSFT]
    Replies:
    0
    Views:
    309
    Jason Cooke [MSFT]
    Jan 30, 2004
  3. Jason Cooke [MSFT]
    Replies:
    0
    Views:
    307
    Jason Cooke [MSFT]
    Jul 1, 2004
  4. Jason Cooke [MSFT]

    [ANN] August 3, "ADO.NET with Visual Basic .NET" chat

    Jason Cooke [MSFT], Aug 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    382
    Jason Cooke [MSFT]
    Aug 3, 2004
  5. Replies:
    0
    Views:
    2,213
Loading...

Share This Page