轮询是网络实时聊天的方式吗?
我正在尝试在网络上实现自定义实时聊天程序,但我不确定如何处理用户的实时(或接近实时)更新。每秒左右从客户端发送 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用长轮询/服务器端推送/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.
您可以让每个客户端轮询服务器,并在服务器端保持连接打开而不响应。
一旦服务器端检测到消息,该数据就会通过已经打开的连接返回。收到后,您的客户立即发出新请求。
这有一定的复杂性,因为您需要跟踪服务器端哪些连接与哪个会话关联,以及应该响应哪些连接以防止超时。
我实际上从未这样做过,但这应该是最有效的资源利用方式。
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.
没有。使用 RabiitMq 或 ActiveMQ 等排队系统。也检查 mongoDB。
排队系统将提供发布-订阅设施。
Nope. use queuing systems like RabiitMq or ActiveMQ. Check mongoDB too.
A queuing system will give u a publish - subscribe facilities.