EventMachine : How to build a chat system with Rails application

谁都会走 提交于 2019-12-10 05:53:17

问题


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 room 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.


回答1:


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.




回答2:


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.




回答3:


The plain old IRC uses the ping? pong! method, that could also be applied here :D



来源:https://stackoverflow.com/questions/1891297/eventmachine-how-to-build-a-chat-system-with-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!