Revision: 34974
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 31, 2010 11:24 by michaelphipps
Initial Code
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
// where is the socket server?
//$host="dev.rumpetroll.com";
$host="rumpetroll.six12.co";
$port = 8180;
$message = "Hiya";
// set variables
$x=100;
$y=100;
$angle=0;
$tadpoles=array();
// create socket
echo "Create Socket\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create
socket\n");
// connect to server
echo "Connect to server\n";
$result = socket_connect($socket, $host, $port) or die("Could not connect
to server\n");
$handshake = <<<EOF
GET /demo HTTP/1.1
Host: rumpetroll.six12.co
Connection: Upgrade
Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
Sec-WebSocket-Protocol: sample
Upgrade: WebSocket
Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
Origin: http://rumpetroll.six12.co
^n:ds[4U
EOF;
// send string to server
echo "Sending Handshake\n";
socket_write($socket, $handshake, strlen($handshake)) or die("Could not send
data to server\n");
//socket_read ($socket, 1024) or die("Could not read server response\n");
// get server response
//echo "Get Server Response\n";
//$result = socket_read ($socket, 1024) or die("Could not read server
//response\n");
/*
// Get Welcome Message
$welcome = socket_read ($socket, 1024) or die("Could not read server
response\n");
$data = json_decode(unwrap($welcome));
echo "start";
print_r($data);
echo "end";
*/
// Get response header and welcome message
$bytes = @socket_recv($socket, $buffer, 2048, 0);
$lines = explode("\n", $buffer);
$lastline= $lines[count($lines)-1];
$welcome = json_decode(unwrap(substr($lastline, strpos($lastline,"{")-1)));
// show Squark
$return = array (
"type"=>"update",
//"id"=>$data->id,
"angle"=>(double)$angle,
"momentum"=>(double)0,
"x"=>(double)$x,//rand(0,300) - 150,
"y"=>(double)$y,//rand(0,300) - 150,
//"life"=>(int)$data->life,
"name"=>'autobot',
"authorized"=>(bool)false
);
$json = wrap(json_encode($return, JSON_FORCE_OBJECT));
socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
while(true) {
// check for incoming messages
$bytes = @socket_recv($socket, $buffer, 2048, 0);
//$buffer = socket_read($socket, 2048);
if(strlen($buffer) > 0) call_user_func('onmessage', $socket, unwrap($buffer), $welcome);
$return = array (
"type"=>"update",
//"id"=>$data->id,
"angle"=>(double)$angle,
"momentum"=>(double)0,
"x"=>(double)$x,//rand(0,300) - 150,
"y"=>(double)$y,//rand(0,300) - 150,
//"life"=>(int)$data->life,
"name"=>'autobot',//date('l jS \of F Y h:i:s A'),
"authorized"=>(bool)false
);
$json = wrap(json_encode($return, JSON_FORCE_OBJECT));
//socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
//sleep(1);
}
// close socket
echo "Closing Socket\n";
socket_close($socket);
// clean up result
echo "Cleaning Up\n";
$result = trim($result);
$result = substr($result, 0, strlen($result)-1);
// print result to browser
echo $result;
echo "\nEnd\n";
exit;
function onmessage($socket, $msg, $welcome){
global $tadpoles;
$message = json_decode($msg);
if (isset($message->type)){
switch($message->type){
case "message" :
echo ">> ".$msg."\n";
if ($message->id != $welcome->id){
echo $message->id.": ".$message->message."\n";
$pattern = "/find: ?(.+)/i";
if (preg_match($pattern, $message->message, $matches, PREG_OFFSET_CAPTURE)){
//echo $matches[1][0];
$index = recursiveArraySearch($tadpoles, $matches[1][0]);
if($index){
$mess = "Follow me to ".$matches[1][0];
global $angle;
// Angle to targetx and targety (mouse position)
$angle = ((atan2($tadpoles[$index]['y']-$y, $tadpoles[$index]['x'] - $x)));
$return = array (
"type"=>"update",
"angle"=>(double)$angle,
"momentum"=>(double)0,
"x"=>(double)$tadpoles[$index]['x']-25,
"y"=>(double)$tadpoles[$index]['y']-25,
"name"=>'autobot',// ('.$x.'/'.$y.':'. $d .')',
"authorized"=>(bool)false
);
$json = wrap(json_encode($return, JSON_FORCE_OBJECT));
socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
}else{
$mess = "I can't find them at the moment";
}
$return = array (
"type"=>"message",
//"id"=>$message->id,
"message"=>$mess,
);
$json = wrap(json_encode($return, JSON_FORCE_OBJECT));
socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
}
}
break;
case "update" :
global $tadpoles;
$tadpoles[$message->id] = array("name" =>$message->name, "x" =>$message->x, "y" =>$message->y);
if ($message->id != $welcome->id){
global $x;
global $y;
global $angle;
$dx=$message->x-$x;
$dy=$message->y-$y;
$d=sqrt($dx*$dx+$dy*$dy);
//echo $d ."\n";
if($d<50 ){
$x=$x+(1*(-$dx/($d)))*200;
$y=$y+(1*(-$dy/($d)))*200;
$x=($x*500+2/2)/501;
$y=($y*500+2/2)/501;
// Angle to targetx and targety (mouse position)
$angle = ((atan2($y - $message->y, $x - $message->x)));
$return = array (
"type"=>"update",
"angle"=>(double)$angle,
"momentum"=>(double)0,
"x"=>(double)$x,
"y"=>(double)$y,
"name"=>'autobot',// ('.$x.'/'.$y.':'. $d .')',
"authorized"=>(bool)false
);
$json = wrap(json_encode($return, JSON_FORCE_OBJECT));
socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
}
}
break;
}
}
}
function wrap($msg="") {
return chr(0).$msg.chr(255);
}
function unwrap($msg="") {
return substr($msg, 1, strlen($msg)-2);
}
function recursiveArraySearch($haystack, $needle, $index = null)
{
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);
while($it->valid())
{
if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
return $aIt->key();
}
$it->next();
}
return false;
}
Initial URL
Initial Description
this is a very basic client for rumpetroll. Very messy, still needs to be turned into a class
Initial Title
Basic PHP Client for Rumpetroll
Initial Tags
php
Initial Language
PHP