Closed boooch closed 2 months ago
有两个方法来修复这个错误
第一种方式,修改 workerman
的源码,inflate_init
函数的 window
参数改为 15
,如下:
/**
* Inflate.
*
* @param $connection
* @param $buffer
* @param $is_fin_frame
* @return false|string
*/
protected static function inflate($connection, $buffer, $is_fin_frame)
{
if (!isset($connection->context->inflator)) {
$connection->context->inflator = \inflate_init(
\ZLIB_ENCODING_RAW,
[
'level' => -1,
'memory' => 8,
'window' => 15,
'strategy' => \ZLIB_DEFAULT_STRATEGY
]
);
}
if ($is_fin_frame) {
$buffer .= "\x00\x00\xff\xff";
}
return \inflate_add($connection->context->inflator, $buffer);
}
/**
* Deflate.
*
* @param $connection
* @param $buffer
* @return false|string
*/
protected static function deflate($connection, $buffer)
{
if (!isset($connection->context->deflator)) {
$connection->context->deflator = \deflate_init(
\ZLIB_ENCODING_RAW,
[
'level' => -1,
'memory' => 8,
'window' => 15,
'strategy' => \ZLIB_DEFAULT_STRATEGY
]
);
}
return \substr(\deflate_add($connection->context->deflator, $buffer), 0, -4);
}
第二种方式,设置响应头:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Websocket;
use Workerman\Worker;
$worker = new Worker('websocket://127.0.0.1:8080');
$worker->onWebSocketConnect = function ($connection) {
// 告诉浏览器服务端支持 permessage-deflate
$connection->headers = [
'sec-websocket-extensions: permessage-deflate; server_no_context_takeover; client_max_window_bits=9' //这个值必须为 9
];
// 服务端设置websocket类型为 permessage-deflate,发送数据会自动压缩
$connection->websocketType = Websocket::BINARY_TYPE_BLOB_DEFLATE;
};
$worker->onMessage = function (TcpConnection $connection, $data)
{
$connection->send('hello');
};
Worker::runAll();
will check both, thank you!
Working well ! Thank you!!
Hi All! I have problem with deflate support on websockets. if short messages from client to server - all working good. But if client send long message to server this long message decoded OK but next messages fail with error. I set this debug line in code:
then start to send messages:
1
window.online_ws.send('137,18,184,199,74,187,141,2,196');
2
window.online_ws.send('b1000_0');
then I send long message from browser:
3
window.online_ws.send('638,935,936,535,969,970,568,639,937,641,640,971,972,973,499,536,537,974,538,539,975,540,976,977,978,541,979,980,981,497,982,983,984,938,498,939,940,941,642,942,943,944,945,946,985,542,986,987,543,988,947,544,948,989,990,991,992,569,993,500,496,994,949,995,996,950,643,951,952,953,954,997,955,956,957,958,959,960,1003,834,998,1002,802,803,861,1374,813,870,876,823,878,493,1369,1371,489,860,1068,1067,871,872,862,863,815,1272,1018,1019,864,1070,612,731,1001,873,804,805,825,816,883,1005,824,1365,961,817,818,1201,906,1020,892,877,907,1066,610,631,567,826,827,908,566,617,857,909,910,911,912,913,914,915,916,917,918,919,865,819,880,920,921,609,821,822,600,607,962,866,922,894,1069,963,923,924,830,925,926,881,898,1006,851,828,927,867,1203,1007,1008,1372,632,633,624,622,553,554,888,728,634,1058,618,623,889,1271,895,896,545,814,884,490,891,1307,1308,1314,1279,1313,807,808,899,858,999,1202,869,635,552,625,608,897,879,893,547,928,929,930,882,1320,1321,829,636,868,492,1009,885,1010,1000,491,1011,801,831,1368,890,820,1012,532,1394,832,886,1013,1064,1014,1015,1016,1367,477,590,964,965,588,587,572,718,645,644,575,585,586,584,592,966,580,593,719,577,591,589,720,967,578,579,721,574,968,722,581,582,723,576,646,724,573,725,637,726,627,887,732,729,730,806,1357,1329,859,1017,874,875,852,853,1366,900,141');
4
ERROR: Aug 21 23:44:36 dev ws_server[1382787]: PHP Warning: inflate_add(): data error in /var/www/html/Pilot7_boooch/cron/vendor/workerman/workerman/Protocols/Websocket.php on line 377
All other messages after this error have thу same error even if they have only 1 symbol.
after reconnection all working well again