用户在聊天室中的状态(通过简单的轮询更新聊天)

发布于 2024-07-16 08:31:31 字数 786 浏览 3 评论 0原文

我们使用简单的 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 技术交流群。

扫码二维码加入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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文