'Sending Messages from PHP to C#
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Devtools |
