Html WebSockets、UDP 和基准测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13040752/
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
WebSockets, UDP, and benchmarks
提问by bobbybee
HTML5 websockets currently use a form of TCP communication. However, for real-time games, TCP just won't cut it (and is great reason to use some other platform, like native). As I probably need UDP to continue a project, I'd like to know if the specs for HTML6 or whatever will support UDP?
HTML5 websockets 目前使用一种 TCP 通信形式。然而,对于实时游戏,TCP 不会削减它(这是使用其他平台的重要原因,如原生)。因为我可能需要 UDP 来继续一个项目,所以我想知道 HTML6 或其他规范是否支持 UDP?
Also, are there any reliable benchmarks for WebSockets that would compare the WS protocol to a low-level, direct socket protocol?
另外,是否有任何可靠的 WebSocket 基准可以将 WS 协议与低级别的直接套接字协议进行比较?
回答by oberstet
On a LAN, you can get Round-trip times for messages over WebSocket of 200 microsec (from browser JS to WebSocket server and back), which is similar to raw ICMP pings. On MAN, it's around 10ms, WAN (over residential ADSL to server in same country) around 30ms, and so on up to around 120-200ms via 3.5G. The point is: WebSocket does add virtually no latency to the one you will get anyway, based on the network.
在 LAN 上,您可以获得 200 微秒的 WebSocket 消息往返时间(从浏览器 JS 到 WebSocket 服务器并返回),这类似于原始 ICMP ping。在 MAN 上,大约为 10 毫秒,WAN(通过住宅 ADSL 到同一国家/地区的服务器)大约为 30 毫秒,依此类推,通过 3.5G 大约为 120-200 毫秒。关键是:基于网络,WebSocket 确实不会为您无论如何都会获得的延迟增加任何延迟。
The wire level overhead of WebSocket (compared to raw TCP) is between 2 octets (unmasked payload of length < 126 octets) and 14 octets (masked payload of length > 64k) per message (the former numbers assume the message is not fragmented into multiple WebSocket frames). Very low.
WebSocket 的线级开销(与原始 TCP 相比)在每条消息 2 个八位字节(长度 < 126 个八位字节的未掩码有效载荷)和 14 个八位字节(长度 > 64k 的掩码有效载荷)之间(前面的数字假设消息没有被分割成多个WebSocket 帧)。非常低。
For a more detailed analysis of WebSocket wire-level overhead, please see this blog post- this includes analysis covering layers beyond WebSocket also.
有关 WebSocket 线级开销的更详细分析,请参阅此博客文章- 这也包括对 WebSocket 之外的层的分析。
More so: with a WebSocket implementation capable of streaming processing, you can (after the initial WebSocket handshake), start a single WebSocket message and frame in each direction and then send up to 2^63 octets with no overhead at all. Essentially this renders WebSocket a fancy prelude for raw TCP. Caveat: intermediaries may fragment the traffic at their own decision. However, if you run WSS (that is secure WS = TLS), no intermediaries can interfere, and there you are: raw TCP, with a HTTP compatible prelude (WS handshake).
更重要的是:使用能够进行流处理的 WebSocket 实现,您可以(在初始 WebSocket 握手之后)在每个方向启动单个 WebSocket 消息和帧,然后发送最多 2^63 个八位字节而完全没有开销。从本质上讲,这使 WebSocket 成为原始 TCP 的奇特前奏。警告:中介可能会自行决定对流量进行分段。但是,如果您运行 WSS(即安全 WS = TLS),则没有中介可以干扰,您就是:原始 TCP,带有 HTTP 兼容前奏(WS 握手)。
WebRTC uses RTP (= UDP based) for media transport but needs a signaling channel in addition (which can be WebSocket i.e.). RTP is optimized for loss-tolerant real-time mediatransport. "Real-time games" often means transferring not media, but things like player positions. WebSocket will work for that.
WebRTC 使用 RTP(= 基于 UDP)进行媒体传输,但另外需要一个信令通道(可以是 WebSocket 即)。RTP 针对丢失容忍的实时媒体传输进行了优化。“实时游戏”通常意味着传输的不是媒体,而是玩家位置之类的东西。WebSocket 将为此工作。
Note: WebRTC transport can be over RTP or secured when over SRTP. See "RTP profiles" here.
注意:WebRTC 传输可以通过 RTP 或通过 SRTP 进行保护。请参阅此处的“RTP 配置文件” 。
回答by kanaka
I would recommend developing your game using WebSockets on a local wired network and then moving to the WebRTC Data Channel API once it is available. As @oberstet correctly notes, WebSocket average latencies are basically equivalent to raw TCP or UDP, especially on a local network, so it should be fine for you development phase. The WebRTC Data Channel API is designed to be very similar to WebSockets (once the connection is established) so it should be fairly simple to integrate once it is widely available.
我建议在本地有线网络上使用 WebSockets 开发您的游戏,然后在 WebRTC 数据通道 API 可用后转移到它。正如@oberstet 正确指出的那样,WebSocket 平均延迟基本上相当于原始 TCP 或 UDP,尤其是在本地网络上,因此对于您的开发阶段应该没问题。WebRTC 数据通道 API 的设计与 WebSockets 非常相似(一旦建立连接),因此一旦广泛可用,集成起来应该相当简单。
Your question implies that UDP is probably what you want for a low latency game and there is truth to that. You may be aware of this already since you are writing a game, but for those that aren't, here is a quick primer on TCP vs UDPfor real-time games:
您的问题暗示 UDP 可能是您想要的低延迟游戏,这是事实。您可能在编写游戏时就已经意识到这一点,但对于那些没有意识到这一点的人,这里有一个关于实时游戏的TCP 与 UDP的快速入门:
TCP is an in-order, reliable transport mechanism and UDP is best-effort. TCP will deliver all the data that is sent and in the order that it was sent. UDP packets are sent as they arrive, may be out of order, and may have gaps (on a congested network, UDP packets are dropped before TCP packets). TCP sounds like a big improvement, and it is for most types of network traffic, but those features come at a cost: a delayed or dropped packet causes all the following packets to be delayed as well (to guarantee in-order delivery).
TCP 是一种有序、可靠的传输机制,而 UDP 则是尽力而为。TCP 将按照发送的顺序传送所有发送的数据。UDP 数据包在到达时发送,可能是乱序的,并且可能有间隙(在拥塞的网络上,UDP 数据包在 TCP 数据包之前被丢弃)。TCP 听起来像是一个很大的改进,它适用于大多数类型的网络流量,但这些功能是有代价的:延迟或丢弃的数据包也会导致所有后续数据包延迟(以保证按顺序交付)。
Real-time games generally can't tolerate the type of delays that can result from TCP sockets so they use UDP for most of the game traffic and have mechanisms to deal with dropped and out-of-order data (e.g. adding sequence numbers to the payload data). It's not such a big deal if you miss one position update of the enemy player because a couple of milliseconds later you will receive another position update (and probably won't even notice). But if you don't get position updates for 500ms and then suddenly get them all out once, that results in terrible game play.
实时游戏通常不能容忍 TCP 套接字可能导致的延迟类型,因此它们使用 UDP 来处理大部分游戏流量,并具有处理丢弃和无序数据的机制(例如,将序列号添加到有效载荷数据)。如果您错过了敌方玩家的一个位置更新,这没什么大不了的,因为几毫秒后您将收到另一个位置更新(甚至可能不会注意到)。但是,如果您在 500 毫秒内没有获得位置更新,然后突然将它们全部清除一次,则会导致糟糕的游戏玩法。
All that said, on a local wired network, packets are almost never delayed or dropped and so TCP is perfectly fine as an initial development target. Once the WebRTC Data Channel API is available then you might consider moving to that. The current proposal has configurable reliability based on retries or timers.
尽管如此,在本地有线网络上,数据包几乎从不延迟或丢弃,因此 TCP 作为初始开发目标完全没问题。一旦 WebRTC 数据通道 API 可用,那么您可能会考虑转向它。当前的提案具有基于重试或计时器的可配置可靠性。
Here are some references:
以下是一些参考:
回答by Alessandro Alinone
To make a long story short, if you want to use TCP for multiplayer games, you need to use what we call adaptive streaming techniques. In other words, you need to make sure that the amount of real-time data sent to synchronize the game world among the clients is governed by the currently available bandwidth and latency for each client.
长话短说,如果您想在多人游戏中使用 TCP,您需要使用我们所说的自适应流技术。换句话说,您需要确保发送以在客户端之间同步游戏世界的实时数据量受每个客户端当前可用的带宽和延迟控制。
Dynamic throttling, conflation, delta delivery, and other mechanisms are adaptive streaming techniques, which don't magically make TCP as efficient as UDP, but make it usable enough for several types of games.
动态节流、合并、增量传输和其他机制是自适应流技术,它们不会神奇地使 TCP 像 UDP 一样高效,但使其足以用于多种类型的游戏。
I tried to explain these techniques in an article: Optimizing Multiplayer 3D Game Synchronization Over the Web(http://blog.lightstreamer.com/2013/10/optimizing-multiplayer-3d-game.html).
我试图在一篇文章中解释这些技术:通过网络优化多人 3D 游戏同步( http://blog.lightstreamer.com/2013/10/optimizing-multiplayer-3d-game.html)。
I also gave a talk on this topic last month at HTML5 Developer Conferencein San Francisco. The video has just been made available on YouTube: http://www.youtube.com/watch?v=cSEx3mhsoHg
上个月我还在旧金山的HTML5 开发者大会上就这个话题发表了演讲。该视频刚刚在 YouTube 上发布:http: //www.youtube.com/watch?v=cSEx3mhsoHg
回答by Timmmm
There's no UDP support for Websockets (there really should be), however you can apparently use WebRTC's RTCDataChannel API for UDP-like communication. There's a good article here:
Websockets 没有 UDP 支持(确实应该有),但是您显然可以使用 WebRTC 的 RTCDataChannel API 进行类似 UDP 的通信。这里有一篇很好的文章:
http://www.html5rocks.com/en/tutorials/webrtc/datachannels/
http://www.html5rocks.com/en/tutorials/webrtc/datachannels/
RTCDataChannel actually uses SCTP which has configurable reliability and ordered delivery. You can get it to act like UDP by telling it to deliver messages unordered, and setting the maximum number of retransmits to 0.
RTCDataChannel 实际上使用了 SCTP,它具有可配置的可靠性和有序交付。您可以通过告诉它无序传递消息并将最大重传次数设置为 0 来使其像 UDP 一样工作。
I haven't tried any of this though.
我还没有尝试过这些。
回答by robertc
I'd like to know if the specs for HTML6 or whatever will support UDP?
我想知道 HTML6 或其他规范是否支持 UDP?
WebSockets won't. One of the benefits of WebSockets is that it piggybacks the existing HTTP connection. This means that to proxies and firewalls WebSockets looks like HTTP so they don't get blocked.
WebSockets 不会。WebSockets 的好处之一是它搭载了现有的 HTTP 连接。这意味着对于代理和防火墙来说,WebSockets 看起来像 HTTP,因此它们不会被阻止。
It's likely arbitrary UDP connections will never be part of any web specification because of security concerns. The closest thing to what you're after will likely come as part of WebRTCand it's associated JSEP protocol.
出于安全考虑,任意 UDP 连接可能永远不会成为任何 Web 规范的一部分。与您所追求的最接近的东西可能会作为WebRTC 的一部分出现,并且它与JSEP 协议相关联。
are there any reliable benchmarks ... that .. compare the WS protocol to a low-level, direct socket protocol?
是否有任何可靠的基准测试……将 WS 协议与低级直接套接字协议进行比较?
Not that I'm aware of. I'm going to go out on a limb and predict WebSockets will be slower ;)
不是我所知道的。我打算冒险并预测 WebSockets 会更慢 ;)