PHP code example of zhamao / connection-manager
1. Go to this page and download the library: Download zhamao/connection-manager library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zhamao / connection-manager example snippets
$result = ZM\ConnectionManager\ManagerGM::init(1024);
$result = ZM\ConnectionManager\ManagerGM::init(1024, 0.2, [
[
"key" => 'connectToken',
"type" => 'string',
"size" => 128
]
]);
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 = 3;
ZM\ConnectionManager\ManagerGM::pushConnect($fd, 'wechatBot', ["connectToken" => 'abcde']);
function onClose($server, $fd) {
ZM\ConnectionManager\ManagerGM::popConnect($fd);
}
$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");