WebSockets适用于实时多人游戏吗?

时间:2022-04-16 03:55:10

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)?

在相关主题上,为此目的有没有更好的WebSockets替代品(JavaScript中的实时多人游戏)?

5 个解决方案

#1


33  

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是最快/最好的解决方案。

#2


7  

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 BOSH or Bayeux are sophisticated but introduces network overhead and latency. The websocket was designed to overcome their limitation and is definitely more responsive.

诸如BOSH或Bayeux之类的HTTP轮询很复杂,但会引入网络开销和延迟。 websocket旨在克服其限制,并且肯定更具响应性。

Libraries, such as cometd or 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.

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

I coded multiplayer arcade game with 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在lan上测量约30ms。这对于多人游戏来说已经足够了。

I suggest you to have a look to nodejs and 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]借用一些多人游戏代码。

#3


7  

I'm not sure if WebSockets are still the best tool for networking a real-time multiplayer these days (2017). WebRTC is 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-webrtc simplifies server-side networking
  • node-webrtc简化了服务器端网络

  • webrtc-native which also provides a server-side library, and could be faster as its name suggests
  • webrtc-native也提供服务器端库,并且可能更快,顾名思义

  • electron-webrtc provides an implementation which is a good match if you want to package your game using electron
  • 如果你想用电子打包游戏,electron-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。 (免责声明:我是贡献者之一)。

#4


3  

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.io use 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的应用正在快速扩展,并且在您完成游戏时可能实际上更为可取,因此需要考虑这一点。

#5


0  

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,那就想想微软开发的Signal R系统。他们正在使用WebSocket,但他们也有一些后备选项...所以协议将使用可用的最佳解决方案。

http://signalr.net/

#1


33  

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是最快/最好的解决方案。

#2


7  

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 BOSH or Bayeux are sophisticated but introduces network overhead and latency. The websocket was designed to overcome their limitation and is definitely more responsive.

诸如BOSH或Bayeux之类的HTTP轮询很复杂,但会引入网络开销和延迟。 websocket旨在克服其限制,并且肯定更具响应性。

Libraries, such as cometd or 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.

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

I coded multiplayer arcade game with 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在lan上测量约30ms。这对于多人游戏来说已经足够了。

I suggest you to have a look to nodejs and 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]借用一些多人游戏代码。

#3


7  

I'm not sure if WebSockets are still the best tool for networking a real-time multiplayer these days (2017). WebRTC is 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-webrtc simplifies server-side networking
  • node-webrtc简化了服务器端网络

  • webrtc-native which also provides a server-side library, and could be faster as its name suggests
  • webrtc-native也提供服务器端库,并且可能更快,顾名思义

  • electron-webrtc provides an implementation which is a good match if you want to package your game using electron
  • 如果你想用电子打包游戏,electron-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。 (免责声明:我是贡献者之一)。

#4


3  

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.io use 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的应用正在快速扩展,并且在您完成游戏时可能实际上更为可取,因此需要考虑这一点。

#5


0  

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,那就想想微软开发的Signal R系统。他们正在使用WebSocket,但他们也有一些后备选项...所以协议将使用可用的最佳解决方案。

http://signalr.net/