while (!feof($fp)) {
$stat = fstat($fp);
$fp2 = fread($fp, 8192);
$new = time();
$fp2 = decode($fp2);
if ($fp2['opcode'] == 9) {
fwrite($fp, encode($new, 'ping'));
echo "\nPING\n\n";
};
if (isset(json_decode($fp2['message'])->data->p)) $fp3 = json_decode($fp2['message'])->data->p;
if (isset($fp3)) echo $fp3."\n";
}
Decode($data)
{
if ($data == false) return;
$bytes = $data;
$dataLength = '';
$mask = '';
$coded_data = '';
$decodedData = '';
$secondByte = sprintf('%08b', ord($bytes[1]));
$masked = ($secondByte[0] == '1') ? true : false;
$dataLength = ($masked === true) ? ord($bytes[1]) & 127 : ord($bytes[1]);
if($masked === true)
{
if($dataLength === 126)
{
$mask = substr($bytes, 4, 4);
$coded_data = substr($bytes, 8);
}
elseif($dataLength === 127)
{
$mask = substr($bytes, 10, 4);
$coded_data = substr($bytes, 14);
}
else
{
$mask = substr($bytes, 2, 4);
$coded_data = substr($bytes, 6);
}
for($i = 0; $i < strlen($coded_data); $i++)
{
$decodedData .= $coded_data[$i] ^ $mask[$i % 4];
}
}
else
{
if($dataLength === 126)
{
$decodedData = substr($bytes, 4);
}
elseif($dataLength === 127)
{
$decodedData = substr($bytes, 10);
}
else
{
$decodedData = substr($bytes, 2);
}
}
return $decodedData;
public function read(): Message
{
$msg = new Message();
do {
// Read header
$header = $this->stream->read(2);
$first = \ord($header[0]);
$second = \ord($header[1]);
$msg->type = $first & 0x0F; // 15/1111
$msg->fin = $first & 0x80; // 128/10000000
$msg->isMasked = $second & 0x80;
$msg->length = $second & 0x7F; // 127/1111111
// Get payload length extensions
if ($msg->length >= 0x7E) { // 126/1111110
$lengthExt = $msg->length == 0x7F ? 8 : 2;
$header = $this->stream->read($lengthExt);
// Set extented paylod length
$msg->length = 0;
for ($i = 0; $i < $lengthExt; $i++) {
$msg->length += \ord($header[$i]) << ($lengthExt - $i - 1) * 8;
}
}
// Get Mask key
if ($msg->isMasked) {
$mask = $this->stream->read(4);
}
// Get payload
$page = '';
while ($msg->length > 0) {
$frame = $this->stream->read($msg->length);
$msg->length -= \strlen($frame);
$page .= $frame;
}
switch ($msg->type) {
case Message::TYPE_PING:
$this->write(Message::create('', Message::TYPE_PONG, true));
break;
case Message::TYPE_CLOSE:
$this->stream->close();
break;
case Message::TYPE_BINARY:
case Message::TYPE_TEXT:
case Message::TYPE_CONTINUE:
if ($msg->isMasked) {
// Unmask data
for ($i = 0, $l = \strlen($page); $i < $l; $i++) {
$msg->data .= $page[$i] ^ $mask[$i % 4];
}
} else {
$msg->data .= $page;
}
break;
}
} while (!$msg->fin);
return $msg;
}
if ($msg->length >= 0x7E) { // 126/1111110
$lengthExt = $msg->length == 0x7F ? 8 : 2;
$header = $this->stream->read($lengthExt);
// Set extented paylod length
$msg->length = 0;
for ($i = 0; $i < $lengthExt; $i++) {
$msg->length += ord($header[$i]) << ($lengthExt - $i - 1) * 8;
}
}
0x7F
или 8 : 2
и присваиваем к lengthExt
, Потом с этой длиной перевыполняем данную функцию? Обнуляем длину и собираем её? 126 {"тело"}
126 {"тело"}~{"тело"}
ssl://stream.binance.com:9443/stream?streams=bnbbusd@kline_5m
, если вы на лету там подписки не меняете. 19 Too many requests
toxa@acer:mylib.acer$ php bin/cli.php websocket runner client btcusdt aggTrade
[2022-05-22 12:28:59] 0.4Mb | [2022-05-22 12:28:59] Start websocket runner client
[2022-05-22 12:29:00.954361] BTCUSDT | Price: 29588.430000 | Quantity: 0.013
[2022-05-22 12:29:01.577557] BTCUSDT | Price: 29588.420000 | Quantity: 0.001
[2022-05-22 12:29:01.712629] BTCUSDT | Price: 29588.420000 | Quantity: 0.004
[2022-05-22 12:29:01.918114] BTCUSDT | Price: 29588.420000 | Quantity: 0.001
[2022-05-22 12:29:02.803572] BTCUSDT | Price: 29588.430000 | Quantity: 0.001
[2022-05-22 12:29:03.644207] BTCUSDT | Price: 29588.430000 | Quantity: 0.035
[2022-05-22 12:29:03.837587] BTCUSDT | Price: 29588.420000 | Quantity: 0.001
[2022-05-22 12:29:04.235999] BTCUSDT | Price: 29588.430000 | Quantity: 0.004
[2022-05-22 12:29:04.569596] BTCUSDT | Price: 29588.430000 | Quantity: 0.002
[2022-05-22 12:29:05.357045] BTCUSDT | Price: 29588.430000 | Quantity: 0.004
[2022-05-22 12:29:05.369365] BTCUSDT | Price: 29588.420000 | Quantity: 0.004
[2022-05-22 12:29:05.725119] BTCUSDT | Price: 29588.430000 | Quantity: 0.004
[2022-05-22 12:29:06.057697] BTCUSDT | Price: 29588.420000 | Quantity: 0.012
[2022-05-22 12:29:06.242477] BTCUSDT | Price: 29588.430000 | Quantity: 0.040
[2022-05-22 12:29:06.422140] BTCUSDT | Price: 29588.420000 | Quantity: 0.009
[2022-05-22 12:29:06.591164] BTCUSDT | Price: 29588.430000 | Quantity: 0.000
[2022-05-22 12:29:06.819583] BTCUSDT | Price: 29588.430000 | Quantity: 0.000
[2022-05-22 12:29:07.135043] BTCUSDT | Price: 29588.430000 | Quantity: 0.002
[2022-05-22 12:29:07.208470] BTCUSDT | Price: 29588.430000 | Quantity: 0.000
[2022-05-22 12:29:07.304731] BTCUSDT | Price: 29588.430000 | Quantity: 0.034
[2022-05-22 12:29:07.424269] BTCUSDT | Price: 29588.430000 | Quantity: 0.017
[2022-05-22 12:29:07.424867] BTCUSDT | Price: 29588.430000 | Quantity: 0.017
[2022-05-22 12:29:07.640549] BTCUSDT | Price: 29588.420000 | Quantity: 0.023
[2022-05-22 12:29:07.725336] BTCUSDT | Price: 29588.430000 | Quantity: 0.001
[2022-05-22 12:29:07.774267] BTCUSDT | Price: 29588.430000 | Quantity: 0.001
[2022-05-22 12:29:08.360303] BTCUSDT | Price: 29588.420000 | Quantity: 0.003
[2022-05-22 12:29:09.115353] BTCUSDT | Price: 29588.420000 | Quantity: 0.000
^C