实施实时投票系统
我正在考虑在我的网站上实施实时投票系统。该网站提供直播,我希望能够在主持人发起的投票期间提示观众选择答案。我可以了解如何将数据存储在 mySQL 数据库中,以及如何处理答案。但是:
我最初如何在客户端开始投票并显示它?脚本是否应该每隔几秒在页面上运行一次,检查另一个页面以查看用户是否可以提出问题?
是否有实时轮询系统的现有示例,例如我正在考虑实施的系统?
I'm looking at implementing a live voting system on my website. The website provides a live stream, and I'd like to be able to prompt viewers to select an answer during a vote initiated by the caster. I can understand how to store the data in a mySQL database, and how to process the answers. However:
How would I initially start the vote on the client-side and display it? Should a script be running every few seconds on the page, checking another page to see if a question is available for the user?
Are there any existing examples of a real-time polling system such as what I'm looking at implementing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须每隔几秒钟向服务器查询一个新问题。
另一种方法是保持连接打开,直到服务器发送更多数据或超时,这只是减少(但不能消除)服务器命中。我认为这就是所谓的“长轮询”。 http://en.wikipedia.org/wiki/Push_technology
You would have to query the server for a new question every few seconds.
The alternative is to hold the connection open until the server sends more data or it times out, which just reduces (but does not eliminate) the server hits. I think it is called "long polling". http://en.wikipedia.org/wiki/Push_technology
您必须从客户端发起连接。最简单的解决方案是让页面每秒左右发出一次 AJAX 请求。网页不必立即返回(它们可能需要 30 秒或更长时间才能响应,而不会导致连接超时)。打开一个连接,直到有话要说才响应,这就是“长轮询”。
You will have to originate the connection from the client-side. The simplest solution is to have the page make an AJAX request every second or so. Web pages don't have to return immediately (they can take 30 seconds or more before responding without the connection timing out). This, opening one connection which doesn't respond until it has something to say, is "long-polling".
您可以在 JavaScript 中使用
setTimeout
每隔几秒发出一次 AJAX 请求,以检查是否有新问题。是的,长轮询可能会更好,但我确信它会更复杂一些。因此,如果您能胜任这项工作,请继续使用它!
以下是有关该主题的更多信息:
http://www.webdevelopmentbits.com/avoiding-long-polling
You could use
setTimeout
in JavaScript to make AJAX requests each few seconds to check whether there are new questions.Yes, long polling might be better, but I'm sure it's a bit more complex. So if you are up to the job, go ahead and use it!
Here's a bit more info on the topic:
http://www.webdevelopmentbits.com/avoiding-long-polling