为网页实现类似“服务器推送”行为的最简单方法是什么?

发布于 2024-07-20 18:41:17 字数 393 浏览 5 评论 0原文

我正在尝试为我的 Django 网站创建一个非常简单的 2 人聊天室。 我正在学习 AJAX 来做到这一点。 我需要在没有用户交互的情况下更新网页的部分内容:

  • 当用户 1 发送消息时,它应该出现在用户 2 的屏幕上
  • 当用户 1 注销时,他的状态应该在用户 2 的屏幕上更改为“离线”

最简单的方法是什么来实施这个? 难道只是使用 JavaScript 定期轮询服务器吗? 如果是这样,这样做的最佳实践是什么? 我找到了一些有关使用 setInterval 或 setTimeout 的解决方案,但不确定这是否是最好的方法。

(我读过有关反向 AJAX 的内容,但从我读到的内容来看,实现起来更加复杂。我不需要我的解决方案快速或可扩展;我只需要它能够工作。)

I'm attempting to make a very simple 2-person chatroom for my Django site. I'm learning AJAX to do this. I need parts of the web page to update without user interaction:

  • When User 1 sends a message, it should appear on User 2's screen
  • When User 1 logs off, his status should change to "offline" on User 2's screen

What is the simplest way to implement this? Is it just to use JavaScript to poll the server at regular intervals? If so, what is the best practice for doing this? I found a few solutions about using setInterval or setTimeout, but wasn't sure if this was the best way.

(I've read about reverse AJAX, but from what I've read this is more complex to implement. I don't need my solution to be fast or scalable; I just need it to work.)

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

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

发布评论

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

评论(3

梦里寻她 2024-07-27 18:41:17

既然你说你不关心 Comet,这确实不是很容易要开始,setInterval 就可以了。 不过,你想对此保持聪明态度。 您可能应该以 30 秒的间隔开始。 如果用户开始聊天,您的时间间隔应减少到 5。当您注意到操作越来越少时,您的计时器应再次减少到 30 秒,依此类推。 它不会在可扩展性大会上赢得任何奖项,但它将为您的普通中型网站完成任务。

有关此技术的更多信息,查看此相关问题

Since you said you don't care for Comet, which is admittedly not very easy to get going, setInterval will do the trick. You want to be smart about it, though. You should probably start with an interval of 30 seconds. If the user starts chatting, your interval should drop down to 5. As you notice less and less action, your timer should decay down to 30 seconds again, and so on. It's not going to win any awards at a scalability convention, but it will get it done for your average mid-size website.

For more on this technique, check out this related question.

别在捏我脸啦 2024-07-27 18:41:17

利用Orbited。 乍一看,您很容易目光呆滞,开始抱怨太复杂,但是一旦您通过了它,您就会明白什么是“最佳实践”,以及为什么它既快速又可扩展。

您还会发现自己意识到这是一项出色的技术,并且可以用于很多动态网页行为。

Take advantage of Orbited. At first glance it's easy for your eyes to glaze over, start mumbling about being too complicated, but once you get passed it you'll understand what the "best practice" is, and why it is both fast and scalable.

You'll also find yourself realizing that this is an outstanding technique, and can be used for lots of dynamic web page behavior.

塔塔猫 2024-07-27 18:41:17

另一种半途而废的选择是 WebSync On-Demand (或任何其他托管彗星服务.. .不确定是否还有其他)。 它可以让您实现服务器推送功能,但工作起来很方便且简单,因为您所需要做的就是包含 javascript 客户端。

One other option that kinda meets you halfway is WebSync On-Demand (or any other hosted comet service...not sure if there are others). It'll let you implement the server-push capabilities, but is nice and simple to get working, since all you need to do is include the javascript client.

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