从PHP发送消息到C#
我正在用C#制作多人游戏游戏,并且使用PHP的服务器。
现在,每次我需要使用C#将请求发送到服务器的服务器时,我都会得到响应,并且只能在发送请求的实例上使用该请求。
在此游戏中,当用户(假设Player1)执行操作时,它运行了将某些内容写入HTML文件的PHP文件。 player2的方法不断运行并检查此文件以在Player2的屏幕上执行Player1的操作。
我认为使用一种方法不断检查文件不是很有效,因为Player1不会每毫秒执行操作,这似乎过多,如果有更多的人连接到游戏,可能会影响服务器。
我的理想情况:
player1 :按Space,该空格在服务器上执行PHP文件,该文件以某种方式向Player2发送“消息”,说“ Player1执行 action”,以便Player2可以更新其屏幕。
Player2 :接收Player1执行的动作并可以使用它。
唯一的问题是我不知道如何将“消息”从PHP发送到Player2的游戏。我不想在不需要时不断运行。
我已经研究了一些,发现可以直接在PHP文件之间创建插座到Player 2的游戏中……如果有可能,我该怎么做?我该如何实现这一目标?
另外,快速注意,我知道有一些框架可以处理这些东西,例如光子,镜像等,但是这些框架无法满足我的需求。
谢谢您的阅读。
I am making a multiplayer game with C# and I have a server using PHP.
Right now as I have it, every time I need to do something with the server I use C# to send a request to the server, then I get response and can use that only on the instance where I sent the request.
In this game when, a user (let's say player1) performs an action, it runs a php file that writes something to an html file. Player2 has a method that constantly runs and checks this file to perform player1's action on player2's screen.
I don't think it is very efficient to have a method constantly checking files, because player1 isn't going to perform an action every millisecond and this seems excessive and probably will affect the server if more people are connected to games.
My ideal situation:
Player1: Presses space, which executes a php file on the server that somehow sends a "message" to player2 saying "player1 performed this action" so player2 can update its screen.
Player2: Receives the action that player1 performed and can use it.
The only problem is I don't know how to send a "message" from php to player2's game. I don't want to have to constantly be running methods when they aren't needed.
I've researched some and found that I can create sockets between the php file directly to player 2's game... if this is a possibility, how can I do this? What ways can I achieve this?
Also, quick note, I know that there are frameworks that handle this stuff like Photon, Mirror, etc., but these don't work with my needs.
Thank you for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题不仅仅是用一段代码来简单回答。
您需要首先了解 TCP/IP 的概念,一旦了解了,您就可以简单地创建 PHP 套接字服务器,它将作为客户端接受来自 C# 程序的连接。这是一个简单的示例(https://www.php.net/manual/ en/sockets.examples.php)
您可以使用实时套接字客户端连接到您的服务器在游戏中进行通信,交换您需要的操作和事件的默认 json 方法。我建议在尝试构建您的游戏之前先研究一下当前的多人游戏是如何工作的。
关于你的最后一个问题,你的玩家 2 可以从套接字获取当前范围和视野中的玩家,基本上你需要在 PHP 中构建一个预结构来加载由游戏本身更新的所有必需参数。然而,这会在稍后引起您对套接字连接保护的其他担忧,但如果它只是出于学习目的,我想它不会有什么区别。
This question has more to it than a simple answer with a piece of code.
You need to understand first the concept of TCP/IP and once you understand that you can simply create PHP socket server which will accept connections from your C# program as a client. Here is a simple example (https://www.php.net/manual/en/sockets.examples.php)
You can communicate within your game using a live socket client connection to your server exchanging your default json methods for the actions and events you need. I would suggest investigating how current multiplayer games work before attemping to build yours.
Regarding your final question your player 2 can get the players in current range and vision from the socket, basically you would need to have a pre structure built within your PHP to load all the required parameters that would be updated by the game itself. However this will raise other concerns for you later in regards to protection of this socket connections, but if it's just for learning purposes i guess it won't mate a difference.