编程语言
首页 > 编程语言> > php应用容器workerman_TcpConnection类pauseRecv接口说明及范例

php应用容器workerman_TcpConnection类pauseRecv接口说明及范例

作者:互联网

pauseRecv接口说明

void Connection::pauseRecv(void)

使当前连接停止接收数据。该连接的onMessage回调将不会被触发。此方法对于上传流量控制非常有用。

pauseRecv接口范例

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('websocket://0.0.0.0:8484');
$worker->onConnect = function($connection)
{
    // 给connection对象动态添加一个属性,用来保存当前连接发来多少个请求
    $connection->messageCount = 0;
};
$worker->onMessage = function(TcpConnection $connection, $data)
{
    // 每个连接接收100个请求后就不再接收数据
    $limit = 100;
    if(++$connection->messageCount > $limit)
    {
        $connection->pauseRecv();
    }
};
// 运行worker
Worker::runAll();

标签:TcpConnection,worker,Worker,workerman,connection,pauseRecv,连接
来源: https://blog.csdn.net/weixin_44039407/article/details/122117317