PHPackages                             zhamao/connection-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zhamao/connection-manager

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zhamao/connection-manager
=========================

A swoole-based multi-process connection manager using shared memory table

1.0.2(5y ago)04.2k1Apache-2.0PHPPHP &gt;=7.2

Since Aug 29Pushed 5y ago2 watchersCompare

[ Source](https://github.com/zhamao-robot/zhamao-connection-manager)[ Packagist](https://packagist.org/packages/zhamao/connection-manager)[ RSS](/packages/zhamao-connection-manager/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (1)

zhamao-connection-manager
=========================

[](#zhamao-connection-manager)

[![https://packagist.org/packages/zhamao/connection-manager](https://camo.githubusercontent.com/2859ed4a24dae9eac8462020f7d44b3ec5e0fb342c9ebcaca359f24b7693fe7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a68616d616f2f636f6e6e656374696f6e2d6d616e61676572)](https://camo.githubusercontent.com/2859ed4a24dae9eac8462020f7d44b3ec5e0fb342c9ebcaca359f24b7693fe7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a68616d616f2f636f6e6e656374696f6e2d6d616e61676572)

一个基于 Swoole\\Table 的简易 Websocket/HTTP 连接属性扩充管理器，兼容 Swoole 多进程的。

A swoole-based multi-process connection manager using shared memory table

安装
--

[](#安装)

通过 Composer

```
composer require zhamao/connection-manager
```

用法
--

[](#用法)

1.在 `Swoole\Server->start()` 前执行初始化：

```
$result = ZM\ConnectionManager\ManagerGM::init(1024);
```

参数如下：

- `$max_conn`: (必填) 设置服务器最大接收的连接属性储存的数目
- `$conflict_proportion`: (选填) hash 冲突的最大百分比 (默认 `0.2`)，有关此参数，见 [Swoole 文档](https://wiki.swoole.com/#/memory/table?id=__construct)。
- `$options`: (选填) 自定义属性参数的键名参数列表，用于添加自定义字段 (默认为空数组)

以下是带丰富参数的用例初始化方式：

```
$result = ZM\ConnectionManager\ManagerGM::init(1024, 0.2, [
    [
        "key" => 'connectToken',
        "type" => 'string',
        "size" => 128
    ]
]);
```

> 参数初始化的必填字段为 `key`，`type`，其中 `type` 限定为以下类型：`string`，`int`，`float`，`int1`，`int2`，`int8`
>
> `size` 属性在 `type` 为 `string` 时必须填写，为字符串的最大长度。储存过程中如果字符串输入过长，会自动截断。

2.在 Swoole 服务器的 `onOpen` 或 `onRequest` 事件最开始执行的代码：

```
use Swoole\Http\Request;
function onOpen($server, Request $request) {
    ZM\ConnectionManager\ManagerGM::pushConnect($request->fd);
}
function onRequest(Request $request, $response) {
    ZM\ConnectionManager\ManagerGM::pushConnect($request->fd);
}
```

参数如下：

- `$fd`: (必填) 设置服务器最大接收的连接属性储存的数目
- `$name`: (选填) 连接对象的类型名称 (默认 `default`)
- `$options`: (选填) 自定义属性参数的键名参数列表，用于添加和设置自定义字段 (默认为空数组)

高级用例：

```
$fd = 3;
ZM\ConnectionManager\ManagerGM::pushConnect($fd, 'wechatBot', ["connectToken" => 'abcde']);
```

3.在 Swoole 服务器的 `onClose` 事件执行的代码（销毁连接对象）：

```
function onClose($server, $fd) {
    ZM\ConnectionManager\ManagerGM::popConnect($fd);
}
```

4.在 Swoole 服务器的 `message` 或 `request` 事件下可以用的方法：

```
$fd = 4;// 从 Frame 或 Request 对象中获取的 $->fd 值。
$obj = ZM\ConnectionManager\ManagerGM::get($fd);
if($obj === null) {
    // 如果 fd 对应的连接不存在，则会返回 null
    echo "连接不存在！" . PHP_EOL;
}
// 函数返回一个 `ConnectionObject` 对象，用于提取数据。
echo $obj->getFd(); // 返回 fd 连接标识符
echo $obj->getName(); // 返回连接类型名称
echo $obj->getOption("connectToken"); // 返回自定义字段的数据
echo $obj->getOptions(); // 返回所有自定义字段的数据
// 对象也可以直接操作改变内容
$obj->setName("qqBot"); // 改变连接的类型名称
$obj->setOption("connectToken", "qwerty"); //改变自定义字段的数据

// 此方法可以获取此类型名称下所有连接的 `ConnectionObject` 对象数组
$conns = ZM\ConnectionManager\ManagerGM::getAllByName("wechatBot");
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Total

4

Last Release

2069d ago

Major Versions

0.1 → 1.02020-08-29

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20330940?v=4)[Jerry Ma](/maintainers/crazywhalecc)[@crazywhalecc](https://github.com/crazywhalecc)

---

Top Contributors

[![crazywhalecc](https://avatars.githubusercontent.com/u/20330940?v=4)](https://github.com/crazywhalecc "crazywhalecc (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zhamao-connection-manager/health.svg)

```
[![Health](https://phpackages.com/badges/zhamao-connection-manager/health.svg)](https://phpackages.com/packages/zhamao-connection-manager)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
