用户在聊天室中的状态(通过简单的轮询更新聊天)
我们使用简单的 Ajax 更新在 Rails 中实现了一个简单的聊天室功能。 现在,在每个聊天室中都有一条消息属于特定用户。 我们想要显示用户列表(例如用户状态)。 请建议方法。 我们没有使用Jabber,XMPP等。
聊天室模型是:
class ChatRoom < ActiveRecord::Base
validates_presence_of :title
has_many :messages,:foreign_key=> "chat_room_id"
has_many :stories,:foreign_key=>"chat_room_id"
has_many :topics,:foreign_key=>"chat_room_id"
end
消息是每个用户发送的聊天内容。
消息模型是:
class Message < ActiveRecord::Base
belongs_to :user
end
用户模型是:
class User < ActiveRecord::Base
acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt
validates_presence_of :nick
validates_uniqueness_of :nick
has_many :questions
end
请建议方法
We have implemented a simple chat room feature in Rails using Simple Ajax updates. Now in every chat room a message belongs to particular user. We want to show the list of users(something like user presence). Please suggest ways. We are not using Jabber,XMPP etc.
The Chatroom model is:
class ChatRoom < ActiveRecord::Base
validates_presence_of :title
has_many :messages,:foreign_key=> "chat_room_id"
has_many :stories,:foreign_key=>"chat_room_id"
has_many :topics,:foreign_key=>"chat_room_id"
end
The messages are the chats sent of every user.
The message model is:
class Message < ActiveRecord::Base
belongs_to :user
end
The USer model is:
class User < ActiveRecord::Base
acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt
validates_presence_of :nick
validates_uniqueness_of :nick
has_many :questions
end
Please suggest ways
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要跟踪哪些用户在哪个房间,您可以在 ChatRoom 和用户模型之间设置 HABTM 关系。 并且,您可以向用户模型添加一个last_poll_datetime 列来跟踪用户上次轮询消息的时间(稍后将详细介绍这部分)。
要显示给定房间中所有用户的列表,请使用 HABTM 连接表 ChatRooms_Users。 每当用户加入或离开房间时,您都将从此表中插入/删除。
如果您想让关闭浏览器而不是单击“离开房间”的用户过期,请设置一个清除任务,每分钟运行一次,查找 last_poll_datetime 早于一分钟的用户,并从 ChatRooms_Users 联接表中删除他们的行。
To keep track of which users are in which room, you could set up a HABTM relationship between the ChatRoom and User models. And, you could add a last_poll_datetime column to the User model to track the last time the user polled for messages (more on this part in a minute).
To show a list of all the users in a given room, use your HABTM join table, ChatRooms_Users. You'll be inserting/deleting from this table whenever a user joins or leaves a room.
If you want to expire users who close their browsers instead of clicking 'leave room', set up a sweeper task to run every minute that looks for users with last_poll_datetime older than one minute and remove their rows from the ChatRooms_Users join table.