-2

This (preferably) is to be accomplished with HTML, Javascript, JSON, PHP, or any key web development language.

I am trying to create a game in which one user's location is sent to another computer in an array/object format, but I have no idea how to accomplish that.

I am trying to avoid using websockets and built-in libraries.

  • Any reason why you can't simply send JSON over XHR / Fetch? Unless you have complex objects that need to be reinstantiated as a particular class, this should be simple enough. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – Markus AO Mar 03 '22 at 08:01
  • Does this answer your question? [Fetch: POST JSON data](https://stackoverflow.com/questions/29775797/fetch-post-json-data) – Markus AO Mar 03 '22 at 08:03
  • Hi Dylan, it's unclear whether you understand whether websocket and built-in libraries can/cannot help you with inter-computer (or inter-process) communication. Websocket is suited for a server initiated protocol in a client-server setup (which traditionally initiated by client). The term "built-in library" is so vague that readers will not know what you referred to. Since you are building a game, it is also likely that you may need some type of peer-to-peer communication instead of client-server. Again, readers need more detail in order to help you. – Michael Chen Mar 07 '22 at 18:36

1 Answers1

0

try this

index.html

<script>
let position = 454;

let data = new FormData();
data.append( "position", JSON.stringify( position ) );

fetch("./test.php",
{
    method: "POST",
    body: data
})

.then(response => response.text())
.then(data => console.log(data));

</script>

test.php

<?php
echo "The position is " . $_POST['position'];
?>

run idex.html the output in your console would be like:

The position is 454
tenshi
  • 508
  • 4
  • 12
  • 1
    That'd work. No need to stringify an integer though! Your answer would be more useful if you added a bit of explanation as to why/how the code solves OP's question. – Markus AO Mar 03 '22 at 13:21