你会如何在 django 中实现聊天功能?

发布于 2024-11-04 06:06:48 字数 255 浏览 3 评论 0 原文

如果你要在 Django 中实现聊天(即实时文本对话),你会如何去做?

我对此的想法(请怜悯菜鸟)-
创建一张用于存储存档消息的表和一个用于存储当前消息的表。
并通过使用 JavaScript 每 20 秒查询当前消息表来填充当前聊天。但这感觉非常错误。
必须有更好的方法。我听说过一种叫做推送技术的东西。如何与 django 一起使用它?
PS 我的网站正在 dreamhost 上共享托管。

If you were to implement Chat (i.e. a real time text conversation) in Django how would you go about doing it ?

My thoughts on it ( please do have mercy on the noob ) -
Create a table for storing archived messages and one for storing current messages.
And populate the current chat by querying the current messages table every 20 secs using javascript. But this feels so wrong.
There has to be a better way. I've heard something called as the push technology. How do I use it with django ?
P.S. My site is being shared hosted on dreamhost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怂人 2024-11-11 06:06:48

大多数网络聊天应用程序倾向于使用 Comet 编程。 Comet并不是真正的推送技术,更多的是对其的模拟。首先,comet 有不同的实现,但通常使用池化和 ajax 来重复检查服务器以查看是否有消息。 Comet 通常会导致过多的往返,因此已采取措施寻找更合适的替代方案,例如仅保持连接开放的 Bosh。这实际上可以被认为是真正的推动,而不是彗星。下面的 wiki 链接列出了替代方案,但总的来说,我认为 Comet 和 Bosh 是最常用的聊天方法。您甚至可以同时使用这两种方法。这里有一些链接。

Most web chat applications tend to use Comet Programming. Comet is not a real push technology, but more of a simulation of it. First off there are different implementation of comet, but generally pooling and ajax is used to repeatedly check the server to see if there is a message. Comet generally results in too many round trips, and therefore steps have been taken to find more suitable alternatives such as Bosh which simply keep the connection open. This could actually be considered real push as opposed to Comet. The wiki link below list alternatives but in generally I think Comet and Bosh are the most used methods for chat. You could possibly even used both methods together. Here are some links.

提赋 2024-11-11 06:06:48

如果你主要关心前端,可以看看 comet。

至于后端,您有很多不同的选择。更直接的方法是将给定聊天室的每条聊天消息存储在数据库中。聊天室表和聊天消息之间可能存在多对多关系。

其他解决方案包括未内置于 Django 中的键值存储数据库。您可以将消息推送到 Redis 集中,或者将活动的聊天室数据存储在 Memcached 中。

由于这是在 dreamhost 上,我会研究数据库选项。事实上,您必须为每条聊天消息存储一些数据。谁制作的、制作时间等。这必须在您环境中的某个时刻进入表格。尝试让它工作,然后看看是否可以让它更快。

If you are mainly concerned with the front end, look into comet.

As for the backend, you have a lot of different options. A more straightforward one would be to store each chat message in the database for the given chat room. Maybe a many to many relation between the chat room table and the chat message.

Other solutions include key value store databases that aren't built in to Django. You could possibly push messages into a redis set, or store active chat room data in memcached.

Since this is on dreamhost, I would look into the database option. The truth is that you have to store some data with each chat message. Who made it, what time it was made, etc. This will have to go into a table at some point in your environment. Try to just make it work and then see if you can make it faster.

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