javascript - 没有 WebSockets,NodeJS 如何处理持久连接?

标签 javascript node.js websocket

我真的是 NodeJS 的新手(如果我对某些事情听起来很幼稚,我很抱歉)并且我一直在深入研究示例的源代码 Chat Application .

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

最佳答案

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

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 不同的所有浏览器。试着看看这个 article :

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.

关于javascript - 没有 WebSockets,NodeJS 如何处理持久连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5428450/

相关文章:

javascript - Jquery:在引用的 js 文件中启用 DOM 就绪函数

javascript - 传递对 XBMC json-rpc 的引用以稍后识别答案

javascript - 使用最新的 discord.js 将用户添加到角色

node.js - Node 代理网络套接字如何检查

java - Spring websocket端点并发送消息

javascript - 如何防止在浏览器外触发onmouseleave事件?

javascript - 无法将 DIV 设置为等于另一个 DIV HTML/Javascript

javascript - 如何创建进度条?

python - 使用套接字(NodeJS,Socket.io)将数据从 Python 发送到 Node 服务器

javascript - 添加动态条件到 mongoose find nodejs.. AND, OR 在同一查询中