长轮询 - 确定何时有新数据的方法是什么?
假设我有一个聊天程序,每次有人发送消息时,都会锁定并写入一个全局文件。接收客户端有一个挂起的 xmlhttp 请求,正在等待使用此构造返回新更新的文件:
while (!hasNewdata())
{ sleep 3; }
print "$thenewdata";
sub hasNewData()
{
# determine if global file has been appended to.
}
将文件大小发送到客户端,然后在下一个轮询请求中返回到服务器是执行此操作的一个好方法,因为我们现在可以检查文件大小是否与已发送回客户端的文件大小不同,以便我们知道有新数据。或者来回发送文件偏移量是否是更好的方法,以便它知道文件中的何处检查新数据?或者完全不同的方法,除了使用全局“聊天”文件之外的其他方法?
确定“可用新数据”的一般方法是什么
Lets say I have a chat program that every time someone sends a message, a globalfile is locked and written to. The receiving client has a pending xmlhttp request that is waiting to return with the newly updated file using this construct:
while (!hasNewdata())
{ sleep 3; }
print "$thenewdata";
sub hasNewData()
{
# determine if global file has been appended to.
}
Would sending the file size to the client and then back to the server in the next poll request be a good method of doing this, as we can now check if the file size is different from what has already been sent back to the client so we know there is new data. Or would sending the file offset back and forth be a better approach so it knows where in the file to check for new data? Or a different method completely, something other than using a global 'chat' file?
What are general methods of determining 'new data available'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用修改日期。
You could use the date modified.
文件大小并不是一个坏方法。
浏览器很可能已经在请求中标记了“if-modified-since”标头。
The file size isn't such a bad way.
Browsers might well already be tagging "if-modified-since" headers to the requests.