NodeJS 如何在没有 WebSockets 的情况下处理持久连接? [英] How does NodeJS handle persistent connections without WebSockets?

查看:30
本文介绍了NodeJS 如何在没有 WebSockets 的情况下处理持久连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 NodeJS 真的很陌生(如果我对某些东西听起来很幼稚,我很抱歉)并且我一直在研究示例的源代码 聊天应用程序.

I'm really new to NodeJS (and I'm sorry if I sound naive about some stuff) and I've been digging into the source code of the example Chat Application.

但是,我无法理解一件事.我知道 WebSockets 有助于处理持久的全双工双向连接.但是 NodeJS 如何在不使用 WebSockets 的情况下管理上述聊天应用程序中的持久连接?如果 NodeJS 可以处理持久的双向连接,那么在 Node 中集成像 Socket.IO 这样的东西的功能究竟是什么?

However, I'm having trouble understanding one thing. I know that WebSockets helps handle persistent full-duplex bi-directional connections. But how does NodeJS manage a persistent connection in the aforementioned chat application without the use of WebSockets? And if NodeJS can handle a persistent bi-directional connection, what exactly is the function of integrating something like Socket.IO in Node?

推荐答案

除了 WebSockets,您还可以使用 long轮询技术在服务器和客户端之间创建持久连接.

Besides WebSockets you can use long polling technique to create persistent connection between server and client.

长轮询是传统的投票技术和允许模拟信息从服务器推送到客户端.和长轮询,客户端请求来自服务器的信息类似于普通民意调查的方式.然而,如果服务器没有任何为客户提供的信息,而不是发送空响应,服务器持有请求并等待以获得一些信息.一旦信息可用(或在适当的超时后),完整的响应被发送到客户.

Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client.

然而它不如 WebSockets 高效,但它被使用是因为它与 WebSockets 不同,它适用于所有浏览器.试着看看这个 文章:

However it's not as efficient as WebSockets, but it's used because it works in every browser unlike WebSockets. Try to look at this article:

将千字节数据减少到 2字节……并将延迟从 150 毫秒减少到到 50ms 远远超过边缘.在事实上,仅这两个因素是足以让 WebSocket 认真起来Google 很感兴趣.

Reducing kilobytes of data to 2 bytes…and reducing latency from 150ms to 50ms is far more than marginal. In fact, these two factors alone are enough to make WebSocket seriously interesting to Google.

这篇关于NodeJS 如何在没有 WebSockets 的情况下处理持久连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆