Websocket:服务器数据是否同步发送? [英] Websocket: are server data sent synchronously?

查看:43
本文介绍了Websocket:服务器数据是否同步发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nodejs + websocket 模块通过 websocket 在客户端和服务器之间建立连接.服务器多次向客户端发送数据:我知道这是一个 TCP 连接,但让我删除所有关于它的疑问.发射"是连续的吗?如果第一个发射"在时间 1s 完成,第二个发射"在时间 2s,那么客户端肯定会收到第一个发射然后第二个发射?如果第一个发射还没有收到而第二个发射了,会发生什么?是否发出阻塞调用?

I'm using nodejs + websocket module to establish a connection between the client and the server through a websocket. The server emits several times data to the client: I know it's a TCP connection, but let me delete every doubt about it. Are the "emits" sequential? If the first "emit" was done at time 1s and the second "emit" at time 2s, will the client receive for sure the first emit and then the second one? What happens if the first emit is not received yet and the second one is emitted? Are emits blocking calls?

推荐答案

WebSockets 基于 TCP.TCP 保证数据包的交付和排序.此外,与 TCP 不同,WebSockets 是基于消息的,这意味着 WebSocket 消息是作为一个完整的消息接收的(TCP 是流式传输的,从侦听器的角度来看,消息"可能会碎片化)

WebSockets is built on TCP. TCP guarantees delivery and ordering of packets. In addition, unlike TCP, WebSockets is message based which means that WebSocket messages are received as an entire message (TCP is streaming and 'messages' may get fragmented from the listener's perspective)

在 node.js 中,从同一个上下文(同一个函数)一个接一个地调用的两个发射将按该顺序传递.但是,如果您的发出在两个不同的回调中,您不能总是保证 Node.js 何时会安排这些回调,因此发出可能会因为计划的回调被重新排序而重新排序.

In node.js, two emits that are called from the same context (the same function) one after the other will be delivered in that order. However, if your emits are in two different callbacks, you can't always guarantee when Node.js will schedule those callbacks and so the emits may get re-ordered because the scheduled callbacks got re-ordered.

更新:

这里有一个例子来扩展为什么 Node.js 的事件驱动性质可能会导致 WebSocket 发出/发送的意外重新排序:

Here is an example to expand on why the event driven nature of Node.js may result in surprising re-ordering of WebSocket emits/sends:

fs.readFile(file1,function(e,data) { ws.send(data); });
fs.readFile(file2,function(e,data) { ws.send(data); }); 

file1 和 file2 传送到浏览器的顺序是不可预测的(由于缓存、文件系统碎片等原因,即使文件大小也不能保证它们何时会触发).即使在 1 秒后使用 setTimeout 调用 file2 的 readFile,浏览器仍可能会乱序接收它们(例如,如果 file1 大得多并且需要 3 秒才能读取,那么 file1 的发送将在发送 file2 之后发生).

The order that file1 and file2 will be delivered to the browser is not predictable (even the file size is not a guarantee of when they will fire due to things like caching, file-system fragmentation, etc). Even if the readFile of file2 is called 1 second later using a setTimeout, the browser may still receive them out of order (e.g. if file1 is much larger and takes 3s to read in then the send of file1 will happen after the send for file2).

所以,是的,浏览器将按照在 Node.js 中调用的顺序接收发送/发送,但由于 Node.js 的异步事件驱动性质,发送/发送可能不会按照您期望的顺序发生.

So yes, emits/sends will be received in the browser in the order they are called in Node.js, but due to the asynchronous event driven nature of Node.js the emits/sends may not happen in the order you expect.

Node.js 的异步事件驱动特性赋予了 Node.js 出色的效率和性能,但如果您不习惯这种基于回调的编程,它可能会产生一些令人惊讶的结果.

The asynchronous event driven nature of Node.js is what gives Node.js excellent efficiency and performance, but if you aren't used to this type of callback based programming it can have some surprising results.

这篇关于Websocket:服务器数据是否同步发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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