Advertisement
Guest User

Ryan Stewart

a guest
Jul 8th, 2010
4,526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. $conn = create_connection('localhost',1740);
  5. $arr_sockets = array($conn);
  6. $handshake = false;
  7.  
  8. while (true)
  9. {
  10.     $changed = $arr_sockets;
  11.     socket_select($changed,$write=NULL,$except=NULL,NULL);
  12.     foreach($changed as $socket)
  13.     {
  14.         echo $socket . " and " . $conn . "\n";
  15.         if($socket == $conn)
  16.         {
  17.             $connection=socket_accept($conn);
  18.             if($connection)
  19.             {
  20.                 array_push($arr_sockets,$connection);              
  21.             }
  22.         } else {
  23.             $bytes = @socket_recv($socket,$buffer,2048,0);
  24.             if(!$handshake)
  25.             {
  26.                 echo "handshaking\n";
  27.                 dohandshake($socket,$buffer);
  28.                 sendStockData($socket);
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. function create_connection($host,$port)
  35. {
  36.     $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
  37.    
  38.     if (!is_resource($socket)) {
  39.         echo 'Unable to create socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
  40.     } else {
  41.         echo "Socket created.\n";
  42.     }
  43.    
  44.     if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
  45.         echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
  46.     } else {
  47.         echo "Set options on socket.\n";
  48.     }
  49.    
  50.     if (!socket_bind($socket, $host, $port)) {
  51.         echo 'Unable to bind socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
  52.     } else {
  53.         echo "Socket bound to port $port.\n";
  54.     }
  55.    
  56.     if (!socket_listen($socket,SOMAXCONN)) {
  57.         echo 'Unable to listen on socket: ' . socket_strerror(socket_last_error());
  58.     } else {
  59.         echo "Listening on the socket.\n";
  60.     }
  61.     return $socket;
  62. }  
  63.  
  64. function dohandshake($socket,$buffer){
  65.     global $handshake;
  66.     list($resource,$host,$origin) = getheaders($buffer);
  67.     $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
  68.                 "Upgrade: WebSocket\r\n" .
  69.                 "Connection: Upgrade\r\n" .
  70.                 "WebSocket-Origin: " . $origin . "\r\n" .
  71.                 "WebSocket-Location: ws://" . $host . $resource . "\r\n" .
  72.                 "\r\n";
  73.     socket_write($socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
  74.     $handshake = true;
  75.     return $handshake;
  76. }
  77.  
  78. function getheaders($req){
  79.   $r=$h=$o=null;
  80.   if(preg_match("/GET (.*) HTTP/"   ,$req,$match)){ $r=$match[1]; }
  81.   if(preg_match("/Host: (.*)\r\n/"  ,$req,$match)){ $h=$match[1]; }
  82.   if(preg_match("/Origin: (.*)\r\n/",$req,$match)){ $o=$match[1]; }
  83.   return array($r,$h,$o);
  84. }
  85.  
  86. function sendStockData($socket)
  87. {
  88.     echo $socket;
  89.     $stock_price = rand(30,32);
  90.     while(true)
  91.     {
  92.         socket_write($socket,chr(0).$stock_price.chr(255),strlen(chr(0).$stock_price.chr(255)));
  93.         echo $stock_price . "sent: " .
  94.         sleep(1);
  95.  
  96.         // Generate a random number that will represent how much our stock price
  97.         // will change and then make that number a decimal and attach it to the
  98.         // previous price.
  99.         $stock_offset = rand(-50,50);
  100.         $stock_price = $stock_price + ($stock_offset/100);
  101. //      echo "$stock_price\n";         
  102.     }
  103.  
  104. }
  105.  
  106. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement