I've been working on this for the last 2 days and I can't come up with a way around it.
Basically heres where my problem is coming in. I'm including the full scripts so you can actually see whats going on.
Problem: I use javascript to catch the keystrokes. I then send it to the coordinate catching process. After it sorts out the cords' I'm trying to send the current coordinate position back to the server. Problem is. I can't get the update process to catch the socket connection. Throws the error the connection isn't set. But in reality it is. I've tried multiple ways and can't get it figured out.
On a side note: I do realize php isn't built for what I'm trying to do. So please do not criticize on it.
Thanks in advanced.
Client_Class.php
c_test.phpPHP Code:<?PHP
class Client {
public function build_enviroment() {
$layout = '
<span id="test">
</span>
<table width="auto" height="auto">
<tr>
<td id="x:1"><img src="imgs/x.jpg"></td>
<td id="x:2"><img src="imgs/O.jpg"></td>
<td id="x:3"><img src="imgs/O.jpg"></td>
</tr>
<tr>
<td id="y:1"><img src="imgs/O.jpg"></td>
<td id="y:2"><img src="imgs/O.jpg"></td>
<td id="y:3"><img src="imgs/O.jpg"></td>
</tr>
</table>
';
echo $layout;
}
}
?>
Start_Client.phpPHP Code:<?PHP
$host = '127.0.0.1';
$port = '1234';
$conn;
function run() {
global $host, $port, $conn;
$GLOBALS['conn'] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($conn, $host, $port);
while(true) {
if($r = socket_read($conn, 2048)) {
tstHandle($r);
}
}
}
function update_cords($cords) {
global $conn;
echo "ran";
socket_send($conn, $cords, strlen($cords), 0);
}
function tstHandle($buffer) {
$data = explode(' ', $buffer);
echo "<span id='cords'>".$data[1].":".$data[2]."</span>";
}
?>
functions.jsPHP Code:<?PHP
include('Client/c_test.php');
if($_GET['cords']) {
$cords = explode(':', $_GET['cords']);
if($cords[0] == "m") {
if($cords[2] != 1) {
$newcords = $cords[1].":".($cords[2] - 1);
update_cords($newcords);
echo $newcords;
}
}
} else {
?>
<html>
<head>
<script type="text/javascript" src="java/functions.js"></script>
<script type="text/javascript">
document.onkeypress = loadKeyFunction;
var cords;
function set_value() {
cords = document.getElementById("cords").innerHTML;
alert(cords);
}
function loadKeyFunction(e)
{
set_value();
if(window.event)
{
var keyId = event.keyCode;
} else if (e)
{
var keyId = e.which;
}
//Left Arrow
if(keyId == 108)
{
if(cords != "x:1" && cords != "y:1") {
document.getElementById(cords).innerHTML="<img src='imgs/o.jpg'>";
window.cords="m:"+cords;
getDataReturnText("?cords=" + cords);
} else {
alert("You cannot move any farther");
}
}
//Right Arrow
else if(keyId == 114
) {
if(cords != "x:3" && cords != "y:3") {
document.getElementById(cords).innerHTML="<img src='imgs/o.jpg'>";
window.cords="p:"+cords;
getDataReturnText("move.php?cords=" + cords);
} else {
alert("You cannot move any farther");
}
}
//Up arrow
else if(keyId == 117) {
if(cords != "x:1" && cords != "x:2" && cords != "x:3") {
document.getElementById(cords).innerHTML="<img src='imgs/o.jpg'>";
window.cords="u:"+cords;
getDataReturnText("move.php?cords=" + cords);
} else {
alert("You cannot move any farther");
}
}
//Down arrow
else if(keyId == 100) {
if(cords != "y:1" && cords != "y:2" && cords != "y:3") {
document.getElementById(cords).innerHTML="<img src='imgs/o.jpg'>";
window.cords="d:"+cords;
getDataReturnText("move.php?cords=" + cords);
} else {
alert("You cannot move any farther");
}
}
else {
alert("key id =" + keyId);
}
}
</script>
</head>
<body>
<span id="test">
</span>
<?PHP
ob_implicit_flush(true);
error_reporting(0);
require_once('Client/Client_Class.php');
$load = new Client();
$load->build_enviroment();
sleep(1);
run();
if(isset($cords)) {
$load->update_cords($cords);
}
?>
</body>
</html>
<?PHP
}
?>
move.phpCode:function getDataReturnText(url)
{
var XMLHttpRequestObject = false;
if(window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();
} else if(window.ActiveXObject)
{
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if(XMLHttpRequestObject)
{
XMLHttpRequestObject.open("GET", url);
XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{
//Need to set this to update cords.
update(XMLHttpRequestObject.responseText);
delete XMLHttpRequestObject;
XMLHttpRequestObject = null;
}
}
XMLHttpRequestObject.send(null);
}
}
function update(c) {
var string = c.split(",");
window.cords=string[0];
document.getElementById(string[0]).innerHTML="<img src='imgs/x.jpg'>";
var insert = "<?PHP $cords = '" + string[1] + "'; echo $cords; ?>"
document.getElementById("test").innerHTML=insert;
}
PHP Code:<?PHP
$cords = explode(':', $_GET['cords']);
//left or right
//$cords[0]
//x
//$cords[1];
//x id
//$cords[2];
if($cords[1] == "x") {
if($cords[0] == "p") {
if($cords[2] != 3) {
$newcords = $cords[1].":".($cords[2] + 1);
$cords = "ud ".$cords[1]." ".($cords[2]+1);
echo $newcords.",".$cords;
}
}
if($cords[0] == "m") {
if($cords[2] != 1) {
$newcords = $cords[1].":".($cords[2] - 1);
echo $newcords;
}
}
if($cords[0] == "d") {
$newcords = "y:".$cords[2];
echo $newcords;
}
}
if($cords[1] == "y") {
if($cords[0] == "p") {
if($cords[2] != 3) {
$newcords = $cords[1].":".($cords[2] + 1);
echo $newcords;
}
}
if($cords[0] == "m") {
if($cords[2] != 1) {
$newcords = $cords[1].":".($cords[2] - 1);
echo $newcords;
}
}
if($cords[0] == "u") {
$newcords = "x:".$cords[2];
echo $newcords;
}
}
?>
