Javascript WebSockets 适合实时多人游戏吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8161053/
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-08-24 04:55:53  来源:igfitidea点击:

Are WebSockets suitable for real-time multiplayer games?

javascriptwebsocketmultiplayer

提问by Josh1billion

I'm interested in building a small real-time multiplayer game, using HTML5/JavaScript for the client and probably Java for the server software.

我对构建一个小型实时多人游戏很感兴趣,客户端使用 HTML5/JavaScript,服务器软件可能使用 Java。

I looked into WebSockets a bit, but it appears I had misconceptions on what WebSockets actually are. I had initially thought of WebSockets as just JavaScript's way of handling TCP sockets, just as they are used in Java and other languages, but it appears there is a whole handshaking process that must take place, and each transmission includes much HTTP overhead (and in that case, the benefits over Ajax do not seem as exciting as at a first glance)?

我稍微研究了 WebSockets,但似乎我对 WebSockets 的实际含义有误解。我最初认为 WebSockets 只是 JavaScript 处理 TCP 套接字的方式,就像它们在 Java 和其他语言中使用一样,但似乎必须发生整个握手过程,并且每次传输都包括很多 HTTP 开销(并且在在这种情况下,与 Ajax 相比的优势似乎不像乍一看那么令人兴奋)?

On a related topic, are there any better alternatives to WebSockets for this purpose (real-time multiplayer games in JavaScript)?

在相关主题上,为此目的(JavaScript 中的实时多人游戏)是否有更好的 WebSocket 替代方案?

采纳答案by leggetter

WebSockets are the best solution for realtime multiplayer games running in a web browser. As pointed out in the comments there is an initial handshake where the HTTP connection is upgraded but once the connection is established WebSockets offer the lowest latency connection mechanism for bi-directional communication between a server and a client.

WebSockets 是在 Web 浏览器中运行的实时多人游戏的最佳解决方案。正如评论中所指出的,有一个初始握手,其中升级了 HTTP 连接,但是一旦建立连接,WebSockets 就为服务器和客户端之间的双向通信提供了最低延迟的连接机制。

I'd recommend you watch this: https://www.youtube.com/watch?v=_t28OPQlZK4&feature=youtu.be

我建议你看这个:https: //www.youtube.com/watch?v=_t28OPQlZK4&feature=youtu.be

Have a look at:

看一下:

The only raw TCP solution would be to use a plugin which supports some kind of TCPClient object. I'd recommend you try out WebSockets.

唯一的原始 TCP 解决方案是使用支持某种 TCPClient 对象的插件。我建议您尝试使用 WebSockets。

You can find a number of options here. Just search for WebSockets within the page.

您可以在此处找到许多选项。只需在页面中搜索 WebSockets。

Also take a look at WebRTC. Depending on the purpose of your game and whether you need your server to manage game state, you could use this technology for peer-to-peer communication. You may still need a solution to handle putting players into groups - in that case WebSockets is the fastest/best solution.

也看看WebRTC。根据您的游戏目的以及您是否需要服务器来管理游戏状态,您可以使用此技术进行点对点通信。您可能仍然需要一个解决方案来处理将玩家分组 - 在这种情况下,WebSockets 是最快/最好的解决方案。

回答by Gary Weiss

I'm not sure if WebSockets are still the best tool for networking a real-time multiplayer these days (2017). WebRTCis a newer technology which offers the potential of much higher performance. And these days, WebRTC is also easier to work with thanks to the following libraries:

我不确定 WebSockets 是否仍然是当今(2017 年)联网实时多人游戏的最佳工具。 WebRTC是一种较新的技术,可提供更高性能的潜力。如今,由于以下库,WebRTC 也更容易使用:

  • node-webrtcsimplifies server-side networking
  • webrtc-nativewhich also provides a server-side library, and could be faster as its name suggests
  • electron-webrtcprovides an implementation which is a good match if you want to package your game using electron
  • node-webrtc简化了服务器端网络
  • webrtc-native也提供了一个服务器端库,正如它的名字所暗示的那样可能会更快
  • 如果您想使用电子打包游戏,则电子 webrtc提供了一个很好的实现

Alternatively, if you want to be spared the actual details of networking implementation, and you're looking for a library which provides a higher-level multiplayer interface, take a look at Lance.gg. (disclaimer: I am one of the contributors).

或者,如果您不想涉及网络实现的实际细节,并且正在寻找提供更高级别多人游戏界面的库,请查看Lance.gg。(免责声明:我是贡献者之一)。

回答by Pierre

Multiplayer games requires the server to send periodic snapshots of the world state to the client. In the context of a browser HTML/js application you have little choices: polling, websocket or write your own plugin to extend browser capabilities.

多人游戏需要服务器定期向客户端发送世界状态的快照。在浏览器 HTML/js 应用程序的上下文中,您几乎没有选择:轮询、websocket 或编写您自己的插件来扩展浏览器功能。

The HTTP polling such as BOSHor Bayeuxare sophisticated but introduces network overhead and latency. The websocket was designed to overcome their limitation and is definitely more responsive.

诸如BOSHBayeux 之类的 HTTP 轮询很复杂,但会引入网络开销和延迟。websocket 旨在克服它们的限制,并且响应速度更快。

Libraries, such as cometdor socket io, provide an abstraction of the transport and solve the browser compatibility issues for you. On top of that, it allows to switch between the underlying transports and compare their performance without effort.

库,例如Cometdsocket io,提供传输的抽象并为您解决浏览器兼容性问题。最重要的是,它允许在底层传输之间切换并毫不费力地比较它们的性能。

I coded multiplayer arcade gamewith socket.io and usual measure 2ms latency with a websocket and around 30ms with xhr-polling on lan. It's enough for a multiplayer games.

我用 socket.io编写多人街机游戏,通常用 websocket 测量 2ms 延迟,在局域网上用 xhr-polling 测量大约 30ms。对于多人游戏来说已经足够了。

I suggest you to have a look to nodejsand socket.io in order to be able to share code between the client and the server, you also car borrow some multiplayer code at [3].

我建议您查看nodejs和 socket.io,以便能够在客户端和服务器之间共享代码,您还可以在 [ 3]处借用一些多人游戏代码。

回答by Josh Langley

Basically, you have 3 options at the time of this writing:

基本上,在撰写本文时,您有 3 个选择:

WebSockets

网络套接字

WebSockets is a lightweight messaging protocol that utilizes TCP, rather than a Javascript implementation of TCP sockets, as you've noted. However, beyond the initial handshake, there are no HTTP headers being passed to and fro beyond that point. Once the connection is established, data passes freely, with minimal overhead.

正如您所指出的,WebSockets 是一种使用 TCP 的轻量级消息传递协议,而不是 TCP 套接字的 Javascript 实现。但是,除了最初的握手之外,没有 HTTP 标头在该点之后来回传递。建立连接后,数据可以自由通过,开销最小。

Long-polling

长轮询

Long-polling, in a nutshell, involves the client polling the server for new information periodically with HTTP requests. This is extremely expensive in terms of CPU and bandwidth, as you're sending a hefty new HTTP header each time. This is essentially your only option when it comes to older browsers, and libraries such as Socket.iouse long-polling as a fallback in these cases.

简而言之,长轮询涉及客户端通过 HTTP 请求定期轮询服务器以获取新信息。这在 CPU 和带宽方面非常昂贵,因为您每次都发送大量的新 HTTP 标头。当涉及到较旧的浏览器时,这基本上是您唯一的选择,并且在这些情况下,诸如Socket.io 之类的库使用长轮询作为后备。

WebRTC

实时时钟

In addition to what has been mentioned already, WebRTC allows for communication via UDP. UDP has long been used in multiplayer games in non web-based environments because of its low overhead (relative to TCP), low latency, and non-blocking nature.

除了已经提到的内容之外,WebRTC 还允许通过 UDP 进行通信。UDP 长期以来一直用于非基于 Web 的环境中的多人游戏,因为它具有低开销(相对于 TCP)、低延迟和非阻塞特性。

TCP "guarantees" that each packet will arrive (save for catastrophic network failure), and that they will always arrive in the order that they were sent. This is great for critical information such as registering scores, hits, chat, and so on.

TCP“保证”每个数据包都会到达(灾难性网络故障除外),并且它们将始终按发送顺序到达。这对于记录分数、点击、聊天等关键信息非常有用。

UDP, on the other hand, has no such guarantees. Packets can arrive in any order, or not at all. This is actually useful when it comes to less critical data that is sent at a high frequency, and needs to arrive as quickly as possible, such as player positions or inputs. The reason being that TCP streams are blocked if a single packet gets delayed during transport, resulting in large gaps in game state updates. With UDP, you can simply ignore packets that arrive late (or not at all), and carry on with the very next one you receive, creating a smoother experience for the player.

另一方面,UDP 没有这样的保证。数据包可以按任何顺序到达,也可以根本不到达。当涉及以高频发送且需要尽快到达的不太重要的数据(例如玩家位置或输入)时,这实际上很有用。原因是如果单个数据包在传输过程中延迟,则 TCP 流将被阻止,从而导致游戏状态更新出现很大差距。使用 UDP,您可以简单地忽略延迟到达(或根本没有到达)的数据包,并继续处理您收到的下一个数据包,从而为玩家创造更流畅的体验。

At the time of this writing, WebSockets are probably your best bet, though WebRTC adoption is expanding quickly, and may actually be preferable by the time you're done with your game, so that's something to consider.

在撰写本文时,WebSockets 可能是您最好的选择,尽管 WebRTC 的采用正在迅速扩大,并且在您完成游戏时实际上可能更可取,因此这是需要考虑的。

回答by MilanG

If you are planing to use JavaScript for your game (as you are) then WebSocket is the best choice for you. And if you want to support older version of Internet Explorer then think of Signal R system Microsoft developed. They are using WebSocket under the hood, but they also have a few fall back options...so protocol will use the best available solution available.

如果您打算在您的游戏中使用 JavaScript(就像您一样),那么 WebSocket 是您的最佳选择。如果您想支持旧版本的 Internet Explorer,请考虑 Microsoft 开发的 Signal R 系统。他们在幕后使用 WebSocket,但他们也有一些后备选项……因此协议将使用可用的最佳解决方案。

http://signalr.net/

http://signalr.net/