英文:
One server that acts as a middleman to connect a pair of clients or server and client for each pair
问题
-
第二种方法是否有特定的名称?
-
哪种方法更好?(考虑到没有太多的重型处理和目前不太担心可伸缩性)
我计划使用JavaScript和Node.js来实现这个,根据这一点,第一种方法似乎更加直接,但我不能仅凭这一点来证明。
英文:
I am developing a web application where two browsers will need to "connect" to each other in order to play a two-player game.
My initial thoughts on how to go ahead with this were to have one central server, which matches up a pair of clients and acts as a sort of "middleman". When browser A wants to make a move, it sends this to the server. The server then sends it to browser B. From my understanding, this is a typical client-server model.
However, an alternative approach would be to have one browser, e.g browser A that acts as both a server and a client and browser B will only act as a client. When browser A makes a move, it simply sends to brower B, (the client) and vice versa. Every time there is another pair of browsers that connect to play the game, one will act as a server and the other will be just a client.
I thought the second approach could be a peer-to-peer approach, however exactly half the clients will be strictly only be a client so I'm not sure if you can define it as a peer-to-peer model.
So my questions are:
-
Is there a specific name for the second approach?
-
What would be the better approach? (given that there isn't much heavy processing and I'm not too worried about scalability as of yet)
I am looking to implement this using javascript and node, and based on that it seems the first approach seems a bit more straightforward, however I cant really justify it apart from that
答案1
得分: 1
浏览器不能托管服务器。您需要拥有一个中央服务器,因为客户端无法相互连接,需要有人提供网页服务。有人需要进行端口转发,这是服务器的工作。
对于问题一,您提到的方法不存在。
对于问题二,最好的方法是让服务器管理一切。但是,您可以让服务器告诉一个客户端执行计算,然后简单地转发它们之间发送的数据包。没有最佳解决方案,这取决于您的服务器性能和游戏的需求有多高。
我建议对于像"四子连珠"这样的基本游戏,主服务器控制实际游戏,这也可以防止作弊。然而,像"谁是卧底"这样有很多活动的游戏将服务器在“主机”客户端和“客户”客户端之间转发数据。
英文:
Browsers cannot host servers. You need to have one central server, as clients cannot connect to each other, and someone needs to serve the webpage itself. Someone needs to port forward, that is the server's job.
For question one, your method doesn’t exist.
For question 2, the best approach is to have the server manage everything. However, you can have the server tell one client to do the calculations and simply forward the packets sent between them. There is no best solution, it depends on how powerful your server is and how demanding your game is.
I would recommend that for a basic game such as connect 4 you have the main server control the actual game, this also prevents cheating. However games like among us with a lot going on will have the server forward stuff between the "host" client and "client" client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论