Posted By

michaelphipps on 10/31/10


Tagged

php socket websocket


Versions (?)

Basic PHP Client for Rumpetroll


 / Published in: PHP
 

this is a very basic client for rumpetroll. Very messy, still needs to be turned into a class

  1. <?php
  2.  
  3.  
  4. // where is the socket server?
  5. //$host="dev.rumpetroll.com";
  6. $host="rumpetroll.six12.co";
  7.  
  8. $port = 8180;
  9. $message = "Hiya";
  10. // set variables
  11. $x=100;
  12. $y=100;
  13. $angle=0;
  14. $tadpoles=array();
  15. // create socket
  16. echo "Create Socket\n";
  17. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create
  18. socket\n");
  19. // connect to server
  20. echo "Connect to server\n";
  21. $result = socket_connect($socket, $host, $port) or die("Could not connect
  22. to server\n");
  23.  
  24.  
  25. $handshake = <<<EOF
  26. GET /demo HTTP/1.1
  27. Host: rumpetroll.six12.co
  28. Connection: Upgrade
  29. Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
  30. Sec-WebSocket-Protocol: sample
  31. Upgrade: WebSocket
  32. Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
  33. Origin: http://rumpetroll.six12.co
  34.  
  35. ^n:ds[4U
  36. EOF;
  37.  
  38. // send string to server
  39. echo "Sending Handshake\n";
  40. socket_write($socket, $handshake, strlen($handshake)) or die("Could not send
  41. data to server\n");
  42.  
  43. //socket_read ($socket, 1024) or die("Could not read server response\n");
  44. // get server response
  45. //echo "Get Server Response\n";
  46. //$result = socket_read ($socket, 1024) or die("Could not read server
  47. //response\n");
  48. /*
  49. // Get Welcome Message
  50. $welcome = socket_read ($socket, 1024) or die("Could not read server
  51. response\n");
  52. $data = json_decode(unwrap($welcome));
  53. echo "start";
  54. print_r($data);
  55. echo "end";
  56.  
  57. */
  58.  
  59.  
  60. // Get response header and welcome message
  61. $bytes = @socket_recv($socket, $buffer, 2048, 0);
  62. $lines = explode("\n", $buffer);
  63. $lastline= $lines[count($lines)-1];
  64.  
  65. $welcome = json_decode(unwrap(substr($lastline, strpos($lastline,"{")-1)));
  66.  
  67.  
  68. // show Squark
  69. $return = array (
  70. "type"=>"update",
  71. //"id"=>$data->id,
  72. "angle"=>(double)$angle,
  73. "momentum"=>(double)0,
  74. "x"=>(double)$x,//rand(0,300) - 150,
  75. "y"=>(double)$y,//rand(0,300) - 150,
  76.  
  77. //"life"=>(int)$data->life,
  78. "name"=>'autobot',
  79. "authorized"=>(bool)false
  80. );
  81. $json = wrap(json_encode($return, JSON_FORCE_OBJECT));
  82. socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
  83.  
  84.  
  85.  
  86.  
  87.  
  88. while(true) {
  89. // check for incoming messages
  90. $bytes = @socket_recv($socket, $buffer, 2048, 0);
  91. //$buffer = socket_read($socket, 2048);
  92. if(strlen($buffer) > 0) call_user_func('onmessage', $socket, unwrap($buffer), $welcome);
  93.  
  94.  
  95. $return = array (
  96. "type"=>"update",
  97. //"id"=>$data->id,
  98. "angle"=>(double)$angle,
  99. "momentum"=>(double)0,
  100. "x"=>(double)$x,//rand(0,300) - 150,
  101. "y"=>(double)$y,//rand(0,300) - 150,
  102.  
  103. //"life"=>(int)$data->life,
  104. "name"=>'autobot',//date('l jS \of F Y h:i:s A'),
  105. "authorized"=>(bool)false
  106. );
  107.  
  108. $json = wrap(json_encode($return, JSON_FORCE_OBJECT));
  109.  
  110. //socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
  111. //sleep(1);
  112. }
  113.  
  114. // close socket
  115. echo "Closing Socket\n";
  116. socket_close($socket);
  117. // clean up result
  118. echo "Cleaning Up\n";
  119. $result = trim($result);
  120. $result = substr($result, 0, strlen($result)-1);
  121. // print result to browser
  122.  
  123. echo $result;
  124.  
  125. echo "\nEnd\n";
  126.  
  127. function onmessage($socket, $msg, $welcome){
  128. global $tadpoles;
  129. $message = json_decode($msg);
  130.  
  131.  
  132.  
  133. if (isset($message->type)){
  134. switch($message->type){
  135. case "message" :
  136. echo ">> ".$msg."\n";
  137.  
  138. if ($message->id != $welcome->id){
  139. echo $message->id.": ".$message->message."\n";
  140.  
  141. $pattern = "/find: ?(.+)/i";
  142. if (preg_match($pattern, $message->message, $matches, PREG_OFFSET_CAPTURE)){
  143. //echo $matches[1][0];
  144. $index = recursiveArraySearch($tadpoles, $matches[1][0]);
  145. if($index){
  146. $mess = "Follow me to ".$matches[1][0];
  147.  
  148. global $angle;
  149. // Angle to targetx and targety (mouse position)
  150. $angle = ((atan2($tadpoles[$index]['y']-$y, $tadpoles[$index]['x'] - $x)));
  151.  
  152.  
  153. $return = array (
  154. "type"=>"update",
  155. "angle"=>(double)$angle,
  156. "momentum"=>(double)0,
  157. "x"=>(double)$tadpoles[$index]['x']-25,
  158. "y"=>(double)$tadpoles[$index]['y']-25,
  159. "name"=>'autobot',// ('.$x.'/'.$y.':'. $d .')',
  160. "authorized"=>(bool)false
  161. );
  162. $json = wrap(json_encode($return, JSON_FORCE_OBJECT));
  163. socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
  164.  
  165.  
  166.  
  167. }else{
  168. $mess = "I can't find them at the moment";
  169. }
  170.  
  171. $return = array (
  172. "type"=>"message",
  173. //"id"=>$message->id,
  174. "message"=>$mess,
  175. );
  176.  
  177. $json = wrap(json_encode($return, JSON_FORCE_OBJECT));
  178.  
  179. socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
  180. }
  181. }
  182. break;
  183. case "update" :
  184. global $tadpoles;
  185. $tadpoles[$message->id] = array("name" =>$message->name, "x" =>$message->x, "y" =>$message->y);
  186.  
  187. if ($message->id != $welcome->id){
  188. global $x;
  189. global $y;
  190. global $angle;
  191.  
  192.  
  193. $dx=$message->x-$x;
  194. $dy=$message->y-$y;
  195. $d=sqrt($dx*$dx+$dy*$dy);
  196. //echo $d ."\n";
  197. if($d<50 ){
  198. $x=$x+(1*(-$dx/($d)))*200;
  199. $y=$y+(1*(-$dy/($d)))*200;
  200.  
  201. $x=($x*500+2/2)/501;
  202. $y=($y*500+2/2)/501;
  203.  
  204. // Angle to targetx and targety (mouse position)
  205. $angle = ((atan2($y - $message->y, $x - $message->x)));
  206.  
  207.  
  208.  
  209. $return = array (
  210. "type"=>"update",
  211. "angle"=>(double)$angle,
  212. "momentum"=>(double)0,
  213. "x"=>(double)$x,
  214. "y"=>(double)$y,
  215. "name"=>'autobot',// ('.$x.'/'.$y.':'. $d .')',
  216. "authorized"=>(bool)false
  217. );
  218. $json = wrap(json_encode($return, JSON_FORCE_OBJECT));
  219. socket_write($socket, $json, strlen($json)) or die("Could not end session\n");
  220.  
  221. }
  222. }
  223. break;
  224. }
  225. }
  226.  
  227.  
  228.  
  229. }
  230.  
  231. function wrap($msg="") {
  232. return chr(0).$msg.chr(255);
  233. }
  234. function unwrap($msg="") {
  235. return substr($msg, 1, strlen($msg)-2);
  236. }
  237.  
  238. function recursiveArraySearch($haystack, $needle, $index = null)
  239. {
  240. $aIt = new RecursiveArrayIterator($haystack);
  241. $it = new RecursiveIteratorIterator($aIt);
  242.  
  243. while($it->valid())
  244. {
  245. if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
  246. return $aIt->key();
  247. }
  248.  
  249. $it->next();
  250. }
  251.  
  252. return false;
  253. }

Report this snippet  

You need to login to post a comment.