From: Jonathan Allen on
Dear List,

I have read over the TCPServer docs and I have been able to implement a
simple server that supports multiple clients and will echo input back to
all users. What I cannot seem to find docs on or grasp, is how to
reference each individual client by a name.

What I wish to accomplish is when a new client connects, they enter
their name/alias so that other users can send messages to individual
users by their name. Also, when they send a line of text to the server,
everyone else sees something like the following.

nocjallen says Hello World!

I'm not sure how to go about doing this. Currently my script creates a
new thread for each connection adds each connection to an array and then
loops through it and sends what one client has said, to each other
connection in the array.

Any helpful input, examples or docs would be greatly appreciated.

Thank you for your help.
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
2010/7/20 Jonathan Allen <jonathan.allen(a)insightbb.com>:
> Dear List,
>
> I have read over the TCPServer docs and I have been able to implement a
> simple server that supports multiple clients and will echo input back to
> all users. What I cannot seem to find docs on or grasp, is how to
> reference each individual client by a name.
>
> What I wish to accomplish is when a new client connects, they enter
> their name/alias so that other users can send messages to individual
> users by their name. Also, when they send a line of text to the server,
> everyone else sees something like the following.
>
> nocjallen says Hello World!
>
> I'm not sure how to go about doing this. Currently my script creates a
> new thread for each connection adds each connection to an array and then
> loops through it and sends what one client has said, to each other
> connection in the array.
>
> Any helpful input, examples or docs would be greatly appreciated.

You need a Hash to store your connections in. Then you must create a
protocol by which every client authenticates itself and sends its
name. You'll likely want to store more than the raw socket in the
Hash, e.g. something like

ClientInfo = Struct.new :socket, :name

Note that you must synchronize access to the centralized Hash which
maps names to ClientInfo instances.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/