I am building a chat system using EventMachine and ruby on rails. It's for learning purpose.
This is how client is connecting to server.
c = TCPSocket.open(ip_address, port)
data = {:user_id => 2, :message => 'hello world'}
c.send(data)
response = c.gets
c.close
It works. However the problem is that I can't get the list of people who are currently chatting in the r开发者_开发技巧oom because as I shown above, client is constantly opening and closing the connection.
An alternative plan is to run an EventMachine client for each connected user. I am planning to store the client connection in session for each user. In this way I will be using the same question for each user. When the user logs out then I will close the connection.
However if the user walks out then how do I self close the client connection.
Any thoughts.
Instead of writing your own, you may be able to build on the Juggernaut library. This is an EventMachine framework which has an example that does precisely this sort of thing.
If the messages are stored within the database, then query it for the users that have written a message in the last 5 minutes. That way if they idle for more than five minutes they're automatically considered out of the chatroom.
The plain old IRC uses the ping? pong! method, that could also be applied here :D
精彩评论