动态状态更新
要实现一项模拟 Twitter 和 Facebook 的功能,允许人们发布状态帖子并自动查看对这些帖子的回复,需要什么?
What is needed to implement a feature that emulates Twitter and Facebook in allowing one to post status posts and seeing responses to these posts coming in automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要查找的术语称为“Comet”,有不同的方法实现它,每个都有许多实现、框架和代码教程。谷歌搜索“Comet”和您计划使用的软件将是一个好的开始。
编辑:
执行这种通信方式的较新技术是“WebSocket ”。它导致 HTTP 连接(比如由 AJAX 请求建立)的行为不太像 HTTP 连接(客户端向服务器发送数据,服务器只能返回数据以响应发送),而更像普通的 TCP连接(双方可以随时发送和接收数据)。
The term you're looking for is called "Comet" and there are different ways of acheiving it, each with many implementations, frameworks and code tutorials. Googling for "Comet" and the software you plan on using will be a good start.
Edit:
A newer technology to perform this style of communication is "WebSocket". It causes an HTTP connection (say as made by an AJAX request) to behave less like an HTTP connection (where the client sends data to the server, and the server can only return data in response to the send) and more like a normal TCP connection (where both sides can send and receive data at any time).
我将结合使用 jQuery $.ajax 命令和 JavaScript setTimeout() 函数每 X 秒轮询一次数据库。这样您就不必刷新屏幕。
I would use a combination of jQuery $.ajax command along with a JavaScript setTimeout() function to poll a database every X seconds. That way you wouldn't have to do a screen refresh.
为了后代的利益,这个问题的新访问者可能想研究一下 WebSocket API 是作为 HTML5 的一部分提出的。目前它有相当可靠的浏览器支持,甚至可以回到 IE 10,所以它应该是安全的足以在现代应用中使用。
For posterity's sake, new visitors to this question may want to look into the WebSocket API that was proposed as part of HTML5. It's got pretty solid browser support at this point, even going back to IE 10, so it should be safe enough to use in modern applications.