轮询是网络实时聊天的方式吗?

发布于 2024-12-11 03:38:51 字数 189 浏览 2 评论 0原文

我正在尝试在网络上实现自定义实时聊天程序,但我不确定如何处理用户的实时(或接近实时)更新。每秒左右从客户端发送 Ajax 请求,轮询数据库以获取新评论是否更有意义?

有没有办法在每次添加评论时以某种方式从数据库进行广播?如果这是可能的,那将如何运作?我正在使用 Sql Server 2008 和 Asp.net (c#)。

谢谢!

I'm trying to implement a custom live chat program on the web, but I'm not sure how to handle the real-time (or near real-time) updates for users. Would it make more sense to send Ajax requests from the client side every second or so, polling the database for new comments?

Is there a way to somehow broadcast from the database each time a comment is added? If this is possible how would that work? I'm using Sql Server 2008 with Asp.net (c#).

Thanks!

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

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

发布评论

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

评论(3

如歌彻婉言 2024-12-18 03:38:51

使用长轮询/服务器端推送/comet:

http://en.wikipedia.org/wiki/Comet_ (编程))

另请参阅:
http://en.wikipedia.org/wiki/Push_technology

我认为当你使用长轮询时还希望您的 Web 服务器以非阻塞 io 的形式为请求提供一些支持,这样您就不会为每个连接占用一个线程。

Use long polling/server side push/comet:

http://en.wikipedia.org/wiki/Comet_(programming))

Also see:
http://en.wikipedia.org/wiki/Push_technology

I think when you use long polling you'll also want your web server to provide some support in the form of non-blocking io for requests, so that you aren't holding a thread per connection.

-残月青衣踏尘吟 2024-12-18 03:38:51

您可以让每个客户端轮询服务器,并在服务器端保持连接打开而不响应。

一旦服务器端检测到消息,该数据就会通过已经打开的连接返回。收到后,您的客户立即发出新请求。

这有一定的复杂性,因为您需要跟踪服务器端哪些连接与哪个会话关联,以及应该响应哪些连接以防止超时。

我实际上从未这样做过,但这应该是最有效的资源利用方式。

You could have each client poll the server, and at the server side keep the connection open without responding.

As soon there is a message detected at server side, this data is returned through the already open connection. On receipt, your client immediately issues a new request.

There's some complexity as you need to keep track server side which connections is associated with which session, and which should be responded upon to prevent timeouts.

I never actually did this but this should be the most resource efficient way.

我喜欢麦丽素 2024-12-18 03:38:51

没有。使用 RabiitMq 或 ActiveMQ 等排队系统。也检查 mongoDB。

排队系统将提供发布-订阅设施。

Nope. use queuing systems like RabiitMq or ActiveMQ. Check mongoDB too.

A queuing system will give u a publish - subscribe facilities.

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