简体   繁体   English

使用HTML / JS / AJAX / Java开发基于多人浏览器的纸牌游戏

[英]Developing a Multiplayer Browser-Based Card Game With HTML/JS/AJAX/Java

I currently have a multiplayer card game developed and working in Java and it is working in the console. 我目前有一个开发和使用Java的多人纸牌游戏,它正在控制台中工作。 The game is similar in format to Bridge and Spades, minus the bidding process. 该游戏的格式类似于Bridge和Spades,减去了出价过程。 It is a four-player game, and players take turns playing a card. 这是一个四人游戏,玩家轮流玩牌。

I am currently working to convert this to a browser-based webapp, and am adding Spring MVC, and using HTML, JavaScript, and AJAX for the UI and communication with the backend. 我目前正在努力将其转换为基于浏览器的webapp,并且正在添加Spring MVC,并使用HTML,JavaScript和AJAX进行UI以及与后端的通信。 I have a good idea of the approach I want to take on getting this to work single-player against the AI, allowing the user to play their card and using an AJAX call to get the next three plays from the server. 我很清楚我想要让这个单人游戏对抗AI的方法,允许用户玩他们的卡并使用AJAX调用从服务器获得接下来的三个游戏。

However, I'm wondering what kind of approach I would need to take for this to be multiplayer. 但是,我想知道我需要采取什么样的方法才能成为多人游戏。 I have seen some references to "Socket-programming," which I am unfamiliar with, but those seem to revolve around Java applets, instead of a browser-based app. 我已经看到一些我不熟悉的“套接字编程”的引用,但这些似乎围绕着Java applet,而不是基于浏览器的应用程序。

Basically, I am looking for a way to ensure that when a user starts a game and someone else joins, how do I ensure that they are connected to the same game, and are able to see each others' plays? 基本上,我正在寻找一种方法来确保当用户开始游戏和其他人加入时,我如何确保他们连接到同一个游戏,并且能够看到彼此的游戏? I am asking this now before I have the UI fully developed for single-player, because I want to avoid a complete redesign to support multiplayer functionality. 在我为单人游戏完全开发UI之前,我现在问这个问题,因为我想避免完全重新设计以支持多人游戏功能。

Since you are creating a multiplayer game, you will need to have at least one server for your client(s) to connect to. 由于您正在创建多人游戏,因此您需要至少有一台服务器供您的客户端连接。 Since you want to make this browser based, you will most likely need your own server (rather than having one of the clients be a server). 由于您希望以此浏览器为基础,因此您很可能需要自己的服务器(而不是让其中一个客户端成为服务器)。 When a user joins a game, it is logged on the server where that user is. 当用户加入游戏时,它将记录在该用户所在的服务器上。 When a player performs an action, the server processes the action, then sends a notification to each of the other clients connected to that room. 当玩家执行操作时,服务器处理该操作,然后向连接到该房间的每个其他客户端发送通知。 At that point the clients UI updates. 此时客户端UI更新。

In the past, you could not do this with pure HTML / JavaScript as you cannot open a socket. 在过去,您无法使用纯HTML / JavaScript执行此操作,因为您无法打开套接字。 Which means, the server could not notify the clients. 这意味着,服务器无法通知客户端。 However in HTML5 you should be able to use WebSockets to achieve what you are doing with a server in the middle. 但是在HTML5中,您应该可以使用WebSockets来实现中间服务器的功能。 The WebSocket API WebSocket API

However, if you don't want to use HTML5 WebSockets, there is another technique that imitates Sockets in JavaScript. 但是,如果您不想使用HTML5 WebSockets,则还有另一种在JavaScript中模仿套接字的技术。 That is, the server can talk to the clients. 也就是说,服务器可以与客户端通信。 This technique is called long polling. 这种技术称为长轮询。 The client sends a request to the server asking for an update, if there is no update available, the server holds the request until an update is available and sends it back to the client at which point they make another update request. 客户端向服务器发送请求更新的请求,如果没有可用更新,则服务器保留请求直到更新可用并将其发送回客户端,此时他们发出另一个更新请求。 Simple Long Polling Example 简单的长轮询示例

Another option, if you are very familiar with Java you may wish to check out Google Web Toolkit . 另一个选择,如果您非常熟悉Java,您可能希望查看Google Web Toolkit GWT is a subset of Java that is compiled into HTML and JavaScript for the front end and if needed creates a Server Side java executable that you can use with TomCat or another web service. GWT是Java的一个子集,可以编译为前端的HTML和JavaScript,如果需要,还可以创建可以与TomCat或其他Web服务一起使用的服务器端Java可执行文件。 In this option, you have a few libraries that allow you to write socket-style code that will be compiled into Long Polling JavaScript. 在此选项中,您有一些库,允许您编写将被编译为Long Polling JavaScript的套接字样式代码。

Best of luck! 祝你好运!

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

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