95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?php
|
|
namespace app\common\controller;
|
|
|
|
use Swoole\Coroutine\MySQL;
|
|
|
|
class Task
|
|
{
|
|
/**
|
|
* 同步用户fd数据
|
|
* @param $data array
|
|
* @return mixed
|
|
* @throws \Swoole\ExitException
|
|
*/
|
|
public function setUserIdServerFd($data)
|
|
{
|
|
$res = true;
|
|
go(function () use ($data, &$res) {
|
|
$mysql = new MySQL();
|
|
$database = require __DIR__ . '/../../../config/database.php';
|
|
$mysql_config = [
|
|
'host' => $database['hostname'],
|
|
'port' => $database['hostport'],
|
|
'user' => $database['username'],
|
|
'password' => $database['password'],
|
|
'database' => $database['database'],
|
|
'charset' => $database['charset'],
|
|
'timeout' => 2
|
|
];
|
|
|
|
// 进行MySQL数据库连接
|
|
$conn_res = $mysql->connect($mysql_config);
|
|
$mysql->setDefer();
|
|
|
|
if ($conn_res === false) {
|
|
echo 'MySQL connect error!' . PHP_EOL;
|
|
var_dump($mysql->connect_errno, $mysql->connect_error) . PHP_EOL;
|
|
throw new \Swoole\ExitException('program exit.');
|
|
}
|
|
|
|
// 设置用户fd链接数据
|
|
$sql = sprintf("UPDATE wc_user SET fd = %s WHERE id = %u", $data['fd'], $data['user_id']);
|
|
$mysql->query($sql);
|
|
|
|
// 一定要记得关闭MySQL连接
|
|
$mysql->close();
|
|
$res = $mysql->recv();
|
|
});
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 清除用户fd数据
|
|
* @param $data array
|
|
* @return mixed
|
|
* @throws \Swoole\ExitException
|
|
*/
|
|
public function delUserIdServerFd($data)
|
|
{
|
|
$res = true;
|
|
go(function () use ($data, &$res) {
|
|
$mysql = new MySQL();
|
|
$database = require __DIR__ . '/../../../config/database.php';
|
|
$mysql_config = [
|
|
'host' => $database['hostname'],
|
|
'port' => $database['hostport'],
|
|
'user' => $database['username'],
|
|
'password' => $database['password'],
|
|
'database' => $database['database'],
|
|
'charset' => $database['charset'],
|
|
'timeout' => 2
|
|
];
|
|
|
|
// 进行MySQL数据库连接
|
|
$conn_res = $mysql->connect($mysql_config);
|
|
$mysql->setDefer();
|
|
|
|
if ($conn_res === false) {
|
|
echo 'MySQL connect error!' . PHP_EOL;
|
|
var_dump($mysql->connect_errno, $mysql->connect_error) . PHP_EOL;
|
|
throw new \Swoole\ExitException('program exit.');
|
|
}
|
|
|
|
// 设置用户fd链接数据
|
|
$sql = sprintf("UPDATE wc_user SET fd = '' WHERE fd = %s", $data['fd']);
|
|
$mysql->query($sql);
|
|
|
|
// 一定要记得关闭MySQL连接
|
|
$mysql->close();
|
|
$res = $mysql->recv();
|
|
});
|
|
|
|
return $res;
|
|
}
|
|
} |