/ Published in: PHP
this is a very basic client for rumpetroll. Very messy, still needs to be turned into a class
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // 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; // create socket echo "Create Socket\n"; socket\n"); // connect to server echo "Connect to server\n"; 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"; 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 // show Squark "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 ); while(true) { // check for incoming messages //$buffer = socket_read($socket, 2048); "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 ); //socket_write($socket, $json, strlen($json)) or die("Could not end session\n"); //sleep(1); } // close socket echo "Closing Socket\n"; // clean up result echo "Cleaning Up\n"; // print result to browser echo $result; echo "\nEnd\n"; exit; function onmessage($socket, $msg, $welcome){ global $tadpoles; switch($message->type){ case "message" : echo ">> ".$msg."\n"; if ($message->id != $welcome->id){ echo $message->id.": ".$message->message."\n"; $pattern = "/find: ?(.+)/i"; //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) "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 ); }else{ $mess = "I can't find them at the moment"; } "type"=>"message", //"id"=>$message->id, "message"=>$mess, ); } } break; case "update" : global $tadpoles; if ($message->id != $welcome->id){ global $x; global $y; global $angle; $dx=$message->x-$x; $dy=$message->y-$y; //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) "type"=>"update", "angle"=>(double)$angle, "momentum"=>(double)0, "x"=>(double)$x, "y"=>(double)$y, "name"=>'autobot',// ('.$x.'/'.$y.':'. $d .')', "authorized"=>(bool)false ); } } break; } } } function wrap($msg="") { } function unwrap($msg="") { } function recursiveArraySearch($haystack, $needle, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); while($it->valid()) { } } return false; }