Advertisement
Skygen

PHP WebSocket Client

Mar 19th, 2015
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Very basic websocket client.
  5.  * Supporting handshake from drafts:
  6.  *  draft-hixie-thewebsocketprotocol-76
  7.  *  draft-ietf-hybi-thewebsocketprotocol-00
  8.  *
  9.  * @author Simon Samtleben <web@lemmingzshadow.net>
  10.  * @version 2011-09-15
  11.  */
  12.  
  13. class WebsocketClient
  14. {
  15.     private $_Socket = null;
  16.  
  17.     public function __construct($host, $port)
  18.     {
  19.         $this->_connect($host, $port); 
  20.     }
  21.  
  22.     public function __destruct()
  23.     {
  24.         $this->_disconnect();
  25.     }
  26.  
  27.     public function sendData($data)
  28.     {
  29.         // send actual data:
  30.         fwrite($this->_Socket, "\x00" . $data . "\xff" ) or die('Error:' . $errno . ':' . $errstr);
  31.         $wsData = fread($this->_Socket, 2000);
  32.         $retData = trim($wsData,"\x00\xff");        
  33.         return $retData;
  34.     }
  35.  
  36.     private function _connect($host, $port)
  37.     {
  38.         $key1 = $this->_generateRandomString(32);
  39.         $key2 = $this->_generateRandomString(32);
  40.         $key3 = $this->_generateRandomString(8, false, true);      
  41.  
  42.         $header = "GET /echo HTTP/1.1\r\n";
  43.         $header.= "Upgrade: WebSocket\r\n";
  44.         $header.= "Connection: Upgrade\r\n";
  45.         $header.= "Host: ".$host.":".$port."\r\n";
  46.         $header.= "Origin: http://foobar.com\r\n";
  47.         $header.= "Sec-WebSocket-Version: 13\r\n";
  48.         $header.= "Sec-WebSocket-Key1: " . $key1 . "\r\n";
  49.         $header.= "Sec-WebSocket-Key2: " . $key2 . "\r\n";
  50.         $header.= "\r\n";
  51.         $header.= $key3;
  52.  
  53.  
  54.         $this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
  55.         fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);
  56.         $response = fread($this->_Socket, 2000);
  57.  
  58.         /**
  59.          * @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
  60.          * See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
  61.          */    
  62.  
  63.         return true;
  64.     }
  65.  
  66.     private function _disconnect()
  67.     {
  68.         fclose($this->_Socket);
  69.     }
  70.  
  71.     private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)
  72.     {  
  73.         $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"ยง$%&/()=[]{}';
  74.         $useChars = array();
  75.         // select some random chars:    
  76.         for($i = 0; $i < $length; $i++)
  77.         {
  78.             $useChars[] = $characters[mt_rand(0, strlen($characters)-1)];
  79.         }
  80.         // add spaces and numbers:
  81.         if($addSpaces === true)
  82.         {
  83.             array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');
  84.         }
  85.         if($addNumbers === true)
  86.         {
  87.             array_push($useChars, rand(0,9), rand(0,9), rand(0,9));
  88.         }
  89.         shuffle($useChars);
  90.         $randomString = trim(implode('', $useChars));
  91.         $randomString = substr($randomString, 0, $length);
  92.         return $randomString;
  93.     }
  94. }
  95.  
  96. $WebSocketClient = new WebsocketClient('178.62.194.140', 8081);
  97. echo $WebSocketClient->sendData('dfghfgh');
  98. unset($WebSocketClient);
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement