您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman回调onBufferFull默认缓冲区大小配置
发布时间:2021-12-02 22:48:28编辑:雪饮阅读()
利用TcpConnection::$defaultMaxSendBufferSize可以设置所有连接默认缓冲区的大小,例如代码:
use Workerman\Connection\TcpConnection;
// 设置所有连接的默认应用层发送缓冲区大小,单位字节
TcpConnection::$defaultMaxSendBufferSize = 2*1024*1024;
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
TcpConnection::$defaultMaxSendBufferSize = 1024;
$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onConnect=function(TcpConnection $connection){
for($i=0;$i<10000;$i++){
$connection->send($i);
}
};
$worker->onBufferFull = function(TcpConnection $connection)
{
echo "id:".$connection->id.",bufferFull and do not send again\n";
};
// 运行worker
Worker::runAll();
?>
实例运行并有两个前端websocket连接时:
[root@izj6c2jeancylo0ppo4vz5z workerman]# php test.php start
Workerman[test.php] start in DEBUG mode
------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.0.22 PHP version:7.0.33
-------------------------------------------- WORKERS ---------------------------------------------
proto user worker listen processes status
tcp root none websocket://0.0.0.0:8484 1 [OK]
--------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
id:1,bufferFull and do not send again
id:2,bufferFull and do not send again
关键字词:workerman,回调,onBufferFull,缓冲区,默认,大小,配置