|
|
@@ -1,5 +1,7 @@
|
|
|
<?php
|
|
|
-namespace app\lib\wclient;
|
|
|
+
|
|
|
+namespace app\lib\wclient;
|
|
|
+
|
|
|
/**
|
|
|
* Created by PhpStorm.
|
|
|
* User: marsnowxiao
|
|
|
@@ -8,7 +10,8 @@ namespace app\lib\wclient;
|
|
|
*/
|
|
|
use app\lib\wclient\WebSocketFrame as WebSocketFrame;
|
|
|
|
|
|
-class PacketHandler {
|
|
|
+class PacketHandler
|
|
|
+{
|
|
|
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
|
const TOKEN_LENGHT = 16;
|
|
|
const maxPacketSize = 2000000;
|
|
|
@@ -32,15 +35,15 @@ class PacketHandler {
|
|
|
return base64_encode($randomString);
|
|
|
}
|
|
|
|
|
|
- public function buildHandShakeRequest($host, $port)
|
|
|
+ public function buildHandShakeRequest($host, $port, $getParas)
|
|
|
{
|
|
|
$this->key = static::generateToken(self::TOKEN_LENGHT);
|
|
|
|
|
|
- return "GET / HTTP/1.1" . "\r\n" .
|
|
|
+ return "GET /{$getParas} HTTP/1.1" . "\r\n" .
|
|
|
"Origin: null" . "\r\n" .
|
|
|
"Host: {$host}:{$port}" . "\r\n" .
|
|
|
"Sec-WebSocket-Key: {$this->key}" . "\r\n" .
|
|
|
- "User-Agent: SwooleWebsocketClient"."/0.1.4" . "\r\n" .
|
|
|
+ "User-Agent: SwooleWebsocketClient" . "/0.1.4" . "\r\n" .
|
|
|
"Upgrade: Websocket" . "\r\n" .
|
|
|
"Connection: Upgrade" . "\r\n" .
|
|
|
"Sec-WebSocket-Protocol: wamp" . "\r\n" .
|
|
|
@@ -60,7 +63,7 @@ class PacketHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return (isset($headerInfo['Sec-WebSocket-Accept']) && $headerInfo['Sec-WebSocket-Accept'] == base64_encode(pack('H*', sha1($this->key.self::GUID))));
|
|
|
+ return (isset($headerInfo['Sec-WebSocket-Accept']) && $headerInfo['Sec-WebSocket-Accept'] == base64_encode(pack('H*', sha1($this->key . self::GUID))));
|
|
|
}
|
|
|
|
|
|
public function processDataFrame(&$packet)
|
|
|
@@ -87,33 +90,28 @@ class PacketHandler {
|
|
|
$length = $handle & 0x7f;
|
|
|
$index++;
|
|
|
//126 short
|
|
|
- if ($length == 0x7e)
|
|
|
- {
|
|
|
+ if ($length == 0x7e) {
|
|
|
if (strlen($packet) < $index + 2)
|
|
|
return null;
|
|
|
//2 byte
|
|
|
$handle = unpack('nl', substr($packet, $index, 2));
|
|
|
$index += 2;
|
|
|
$length = $handle['l'];
|
|
|
- }
|
|
|
- //127 int64
|
|
|
- elseif ($length > 0x7e)
|
|
|
- {
|
|
|
+ } //127 int64
|
|
|
+ elseif ($length > 0x7e) {
|
|
|
if (strlen($packet) < $index + 8)
|
|
|
return null;
|
|
|
//8 byte
|
|
|
$handle = unpack('Nh/Nl', substr($packet, $index, 8));
|
|
|
$index += 8;
|
|
|
$length = $handle['l'];
|
|
|
- if ($length > static::maxPacketSize)
|
|
|
- {
|
|
|
+ if ($length > static::maxPacketSize) {
|
|
|
throw new \Exception("frame length is too big.\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//mask-key: int32
|
|
|
- if ($mask)
|
|
|
- {
|
|
|
+ if ($mask) {
|
|
|
if (strlen($packet) < $index + 4)
|
|
|
return null;
|
|
|
$mask = array_map('ord', str_split(substr($packet, $index, 4)));
|