简体   繁体   English

在基于浏览器的游戏中创建UDP和TCP连接?

[英]Create UDP and TCP connections in a browser-based game?

I'm working on a game and want the freedom to choose between UDP and TCP connections in the future when I decide to add multiplayer. 我正在开发一款游戏,希望将来决定添加多人游戏时可以在UDP和TCP连接之间自由选择。 I'm not sure how something like this would work in the browser with security. 我不确定具有安全性的浏览器将如何运行这种功能。

There are two scenarios I can think of for multiplayer. 对于多人游戏,我可以想到两种情况。 The first is that a player becomes the host of the game (in the browser) and thus would need to communicate with other players directly via TCP or UDP. 首先是玩家成为游戏的主机(在浏览器中),因此需要直接通过TCP或UDP与其他玩家进行通信。 This would save me on bandwidth costs. 这样可以节省带宽成本。

The second scenario would be to have the web server host the game and pass messages back and forth to the clients again via TCP or UDP. 第二种情况是让Web服务器托管游戏并将消息再次通过TCP或UDP传递给客户端。

Do Flash or Java applets let me do something like this? Flash或Java小程序可以让我做这样的事情吗? If not is there an alternative I can use for the browser or am I stuck with moving to providing a game download and installation? 如果没有,我可以将其用于浏览器,还是坚持下载游戏并进行安装?

Both Java applets / web-start applications allow you to establish network connections for gaming etc. Java小程序/网络启动应用程序均允许您建立游戏等的网络连接。

The first is that a player becomes the host of the game (in the browser) and thus would need to communicate with other players directly via TCP or UDP. 首先是玩家成为游戏的主机(在浏览器中),因此需要直接通过TCP或UDP与其他玩家进行通信。

Java enforces a "same-origin-policy" which says that (without special permission from the user) your application may only connect back to the server it came from. Java强制执行一个“ same-origin-policy”,该策略表示(未经用户特别许可)您的应用程序只能连接回它来自的服务器。

AFAIK, same applies for Flash-application. AFAIK,同样适用于Flash应用程序。

The second scenario would be to have the web server host the game and pass messages back and forth to the clients again via TCP or UDP. 第二种情况是让Web服务器托管游戏并将消息再次通过TCP或UDP传递给客户端。

This would probably be the way to go, if you don't want to ask the user for special permissions. 如果您不想询问用户特殊权限,这可能就是解决方法。

Javascript can't do UDP. Javascript无法执行UDP。 Applets can do connections back to their source no matter what, and they can do more network-y stuff if signed, and only if the user enables Java and trusts your certificate. 无论如何,Applet都可以将连接建立回源,并且只有在用户启用Java并信任您的证书的情况下,Applet才能进行更多的网络事务。 Given the recent history of malware applets on facebook, I would't plan using an applet if you want to make a browser-based game. 鉴于Facebook上最近有恶意软件小程序的历史,如果您要制作基于浏览器的游戏,我将不打算使用小程序。

It's possible in Flash: 在Flash中是可能的:

  • Option one with centralised server and end-to-end peering: Using peers (only with UDP) in Flash is possible, but is currently in beta and requires users to have Flash Player 10 or 10.1, depending on your implementation. 带有集中式服务器和端到端对等的选项一:可以在Flash中使用对等点(仅与UDP一起使用),但当前处于beta版本,并且取决于您的实现,要求用户具有Flash Player 10或10.1。 See an example demo game . 参见示例演示游戏

  • Option two with centralised server: You can use Real Time Messaging Protocol (only with TCP). 带有中央服务器的选项二:可以使用实时消息协议 (仅适用于TCP)。 Real-time multi player Flash games usually use this. 实时多人Flash游戏通常使用此功能。 You also have an option to exchange data via HTTP. 您还可以选择通过HTTP交换数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM