perf: 优化ws连接机制

This commit is contained in:
kuaifan 2023-02-22 19:26:56 +08:00
parent 50a6b91a7e
commit db5bcfb683
5 changed files with 15 additions and 24 deletions

View File

@ -81,19 +81,6 @@ class User extends AbstractModel
// 基本信息的字段
public static $basicField = ['userid', 'email', 'nickname', 'profession', 'department', 'userimg', 'bot', 'az', 'pinyin', 'line_at', 'disable_at'];
/**
* 更新数据校验
* @param array $param
*/
public function updateInstance(array $param)
{
parent::updateInstance($param);
//
if (isset($param['line_at']) && $this->userid) {
Cache::put("User::online:" . $this->userid, time(), Carbon::now()->addSeconds(30));
}
}
/**
* 昵称
* @param $value
@ -163,7 +150,7 @@ class User extends AbstractModel
*/
public function getOnlineStatus()
{
$online = $this->bot || intval(Cache::get("User::online:" . $this->userid, 0)) > 0;
$online = $this->bot || Cache::get("User::online:" . $this->userid) === "on";
if ($online) {
return true;
}

View File

@ -1537,10 +1537,8 @@ class Base
*/
public static function jsonEcho($param)
{
global $_GPC;
//
$json = json_encode($param);
$callback = $_GPC['callback'];
$callback = Request::input('callback');
if ($callback) {
return $callback . '(' . $json . ')';
} else {
@ -1557,11 +1555,11 @@ class Base
*/
public static function retSuccess($msg, $data = [], $ret = 1)
{
return array(
return [
'ret' => $ret,
'msg' => self::Lang($msg),
'data' => $data
);
];
}
/**
@ -1573,11 +1571,11 @@ class Base
*/
public static function retError($msg, $data = [], $ret = 0)
{
return array(
return [
'ret' => $ret,
'msg' => self::Lang($msg),
'data' => $data
);
];
}
/**

View File

@ -191,13 +191,15 @@ class WebSocketService implements WebSocketHandlerInterface
*/
private function saveUser($fd, $userid)
{
Cache::put("User::fd:" . $fd, "on", Carbon::now()->addDay());
Cache::put("User::online:" . $userid, "on", Carbon::now()->addDay());
//
WebSocket::updateInsert([
'key' => md5($fd . '@' . $userid)
], [
'fd' => $fd,
'userid' => $userid,
]);
Cache::put("User::online:" . $userid, time(), Carbon::now()->addSeconds(30));
}
/**
@ -206,6 +208,8 @@ class WebSocketService implements WebSocketHandlerInterface
*/
private function deleteUser($fd)
{
Cache::forget("User::fd:" . $fd);
//
$array = [];
WebSocket::whereFd($fd)->chunk(10, function($list) use (&$array) {
/** @var WebSocket $item */
@ -216,6 +220,7 @@ class WebSocketService implements WebSocketHandlerInterface
User::whereUserid($item->userid)->update([
'line_at' => Carbon::now()
]);
Cache::forget("User::online:" . $item->userid);
}
if ($item->path && str_starts_with($item->path, "/single/file/")) {
$array[$item->path] = $item->path;

View File

@ -2786,7 +2786,7 @@ export default {
break
default:
msgId && dispatch("websocketSend", {type: 'receipt', msgId});
msgId && dispatch("websocketSend", {type: 'receipt', msgId}).catch(_ => {});
state.wsMsg = msgDetail;
Object.values(state.wsListener).forEach((call) => {
if (typeof call === "function") {
@ -3055,7 +3055,7 @@ export default {
state.wsPathValue = path;
state.wsPathTimeout = setTimeout(() => {
if (state.wsPathValue == path) {
dispatch("websocketSend", {type: 'path', data: {path}});
dispatch("websocketSend", {type: 'path', data: {path}}).catch(_ => {});
}
}, 1000);
},

View File

@ -64,6 +64,7 @@ export default {
wsListener: {},
wsReadTimeout: null,
wsReadWaitData: {},
wsCallReconnect: 0,
// 会员信息
userInfo: {},