Chinaunix首页 | 论坛 | 博客
  • 博客访问: 914880
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: PHP

2016-06-06 19:32:42


点击(此处)折叠或打开

  1. <?php
  2. $host = '10.9.8.173'; //where is the websocket server
  3. $port = 8575;
  4. $local = ""; //url where this script run
  5. $data = "first message"; //data to be send

  6. $head = "GET / HTTP/1.1"."\r\n".
  7.         "Upgrade: WebSocket"."\r\n".
  8.         "Connection: Upgrade"."\r\n".
  9.         "Origin: $local"."\r\n".
  10.         "Host: $host"."\r\n".
  11.         "Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
  12.         "Content-Length: ".strlen($data)."\r\n"."\r\n";
  13. //WebSocket handshake
  14. $sock = fsockopen($host, $port, $errno, $errstr, 2);
  15. fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
  16. $headers = fread($sock, 2000);
  17. echo $headers;
  18. fwrite($sock, hybi10Encode($data)) or die('error:'.$errno.':'.$errstr);
  19. $wsdata = fread($sock, 2000);
  20. var_dump(hybi10Decode($wsdata));
  21. fclose($sock);


  22. function hybi10Decode($data)
  23. {
  24.     $bytes = $data;
  25.     $dataLength = '';
  26.     $mask = '';
  27.     $coded_data = '';
  28.     $decodedData = '';
  29.     $secondByte = sprintf('%08b', ord($bytes[1]));
  30.     $masked = ($secondByte[0] == '1') ? true : false;
  31.     $dataLength = ($masked === true) ? ord($bytes[1]) & 127 : ord($bytes[1]);

  32.     if($masked === true)
  33.     {
  34.         if($dataLength === 126)
  35.         {
  36.            $mask = substr($bytes, 4, 4);
  37.            $coded_data = substr($bytes, 8);
  38.         }
  39.         elseif($dataLength === 127)
  40.         {
  41.             $mask = substr($bytes, 10, 4);
  42.             $coded_data = substr($bytes, 14);
  43.         }
  44.         else
  45.         {
  46.             $mask = substr($bytes, 2, 4);
  47.             $coded_data = substr($bytes, 6);
  48.         }
  49.         for($i = 0; $i < strlen($coded_data); $i++)
  50.         {
  51.             $decodedData .= $coded_data[$i] ^ $mask[$i % 4];
  52.         }
  53.     }
  54.     else
  55.     {
  56.         if($dataLength === 126)
  57.         {
  58.            $decodedData = substr($bytes, 4);
  59.         }
  60.         elseif($dataLength === 127)
  61.         {
  62.             $decodedData = substr($bytes, 10);
  63.         }
  64.         else
  65.         {
  66.             $decodedData = substr($bytes, 2);
  67.         }
  68.     }

  69.     return $decodedData;
  70. }


  71. function hybi10Encode($payload, $type = 'text', $masked = true) {
  72.     $frameHead = array();
  73.     $frame = '';
  74.     $payloadLength = strlen($payload);

  75.     switch ($type) {
  76.         case 'text':
  77.             // first byte indicates FIN, Text-Frame (10000001):
  78.             $frameHead[0] = 129;
  79.             break;

  80.         case 'close':
  81.             // first byte indicates FIN, Close Frame(10001000):
  82.             $frameHead[0] = 136;
  83.             break;

  84.         case 'ping':
  85.             // first byte indicates FIN, Ping frame (10001001):
  86.             $frameHead[0] = 137;
  87.             break;

  88.         case 'pong':
  89.             // first byte indicates FIN, Pong frame (10001010):
  90.             $frameHead[0] = 138;
  91.             break;
  92.     }

  93.     // set mask and payload length (using 1, 3 or 9 bytes)
  94.     if ($payloadLength > 65535) {
  95.         $payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
  96.         $frameHead[1] = ($masked === true) ? 255 : 127;
  97.         for ($i = 0; $i < 8; $i++) {
  98.             $frameHead[$i + 2] = bindec($payloadLengthBin[$i]);
  99.         }

  100.         // most significant bit MUST be 0 (close connection if frame too big)
  101.         if ($frameHead[2] > 127) {
  102.             $this->close(1004);
  103.             return false;
  104.         }
  105.     } elseif ($payloadLength > 125) {
  106.         $payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
  107.         $frameHead[1] = ($masked === true) ? 254 : 126;
  108.         $frameHead[2] = bindec($payloadLengthBin[0]);
  109.         $frameHead[3] = bindec($payloadLengthBin[1]);
  110.     } else {
  111.         $frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
  112.     }

  113.     // convert frame-head to string:
  114.     foreach (array_keys($frameHead) as $i) {
  115.         $frameHead[$i] = chr($frameHead[$i]);
  116.     }

  117.     if ($masked === true) {
  118.         // generate a random mask:
  119.         $mask = array();
  120.         for ($i = 0; $i < 4; $i++) {
  121.             $mask[$i] = chr(rand(0, 255));
  122.         }

  123.         $frameHead = array_merge($frameHead, $mask);
  124.     }
  125.     $frame = implode('', $frameHead);
  126.     // append payload to frame:
  127.     for ($i = 0; $i < $payloadLength; $i++) {
  128.         $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
  129.     }

  130.     return $frame;
  131. }

  132. ?>

阅读(1429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~