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']; 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 * @param $value
@ -163,7 +150,7 @@ class User extends AbstractModel
*/ */
public function getOnlineStatus() 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) { if ($online) {
return true; return true;
} }

View File

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

View File

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

View File

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

View File

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