javascript 实时消息/更新的长轮询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9953465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:20:22  来源:igfitidea点击:

Long polling for real time messages/updates

c#javascriptasp.net

提问by TGH

What is a good strategy for implementing long polling in a .Net application.

在 .Net 应用程序中实现长轮询的好策略是什么。

Would it mean having a JS setInterval() based approach to keep polling the server for updates that can be rendered on the page. I have always thought that that could be a challenge when it comes to scalability as it seems it would generate a lot of extra requests to the web server. I have read that this type of functionality should be implemented using a non blocking web server (single threaded) NODE.js etc... Since there's only one thread/event loop it seems like the requests would have to be very lightweight to service several requests in a timely fashion. Can Node.Js trigger db calls?

这是否意味着使用基于 JS setInterval() 的方法来不断轮询服务器以获取可以在页面上呈现的更新。我一直认为这在可扩展性方面可能是一个挑战,因为它似乎会向 Web 服务器生成大量额外请求。我已经读过这种类型的功能应该使用非阻塞 Web 服务器(单线程)NODE.js 等来实现......由于只有一个线程/事件循环,因此请求似乎必须非常轻量级才能为多个服务提供服务及时提出要求。Node.Js 可以触发数据库调用吗?

I have seen an online dating site where you receive notification in the form of a fad-in/fade-out popup when someone visits your profile when you're currently logged into the system. I am impressed that something like that can work so well for a high volume site.

我见过一个在线约会网站,当您当前登录系统时有人访问您的个人资料时,您会收到淡入/淡出弹出窗口形式的通知。我印象深刻的是,这样的东西可以在大容量网站上运行得如此好。

Is it reasonable to assume that this type of notification system is implemented using long polling? Based on constantly polling through JS?

假设这种类型的通知系统是使用长轮询实现的是否合理?基于通过 JS 不断轮询?

I am seeing similar behind the scenes updates her on the SO site as well (messages/votes etc) Does this use a similar strategy as well?

我也在 SO 网站上看到类似的幕后更新(消息/投票等)这是否也使用类似的策略?

采纳答案by antonjs

Realtime web applications have been with us for quite some time now: the history of Polling goes from setInterval Techniqueto HTML5 WebSockets.

实时网络应用已经伴随我们相当一段时间:轮询的历史,从去setInterval TechniqueHTML5 WebSockets

Here you can find Simple Long Polling Example with JavaScript.

在这里您可以找到使用 JavaScript 的简单长轮询示例。

http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery

回答by ZeNo