WebSockets 适合实时多人游戏吗? [英] Are WebSockets suitable for real-time multiplayer games?

查看:36
本文介绍了WebSockets 适合实时多人游戏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 替代方案(JavaScript 中的实时多人游戏)?

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

推荐答案

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

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.

我建议您观看此内容:https://www.youtube.com/watch?v=_t28OPQlZK4&feature=youtu.be

看看:

  • http://browserquest.mozilla.org/ code available here: https://github.com/mozilla/BrowserQuest
  • https://chrome.com/supersyncsports/

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

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.

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

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

另请参阅 WebRTC.根据您的游戏目的以及您是否需要服务器来管理游戏状态,您可以使用此技术进行点对点通信.您可能仍然需要一个解决方案来处理将玩家分组 - 在这种情况下,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.

这篇关于WebSockets 适合实时多人游戏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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