From db5bcfb6834e9b04dd5b5b0d374e731df2d6fb87 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Wed, 22 Feb 2023 19:26:56 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96ws=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 15 +-------------- app/Module/Base.php | 12 +++++------- app/Services/WebSocketService.php | 7 ++++++- resources/assets/js/store/actions.js | 4 ++-- resources/assets/js/store/state.js | 1 + 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 67aa801f2..017fac5fc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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; } diff --git a/app/Module/Base.php b/app/Module/Base.php index e6f1f01f6..f68edb22d 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -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 - ); + ]; } /** diff --git a/app/Services/WebSocketService.php b/app/Services/WebSocketService.php index 26b5a38e8..104429848 100644 --- a/app/Services/WebSocketService.php +++ b/app/Services/WebSocketService.php @@ -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; diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index a3cdb5495..3e617d28c 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -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); }, diff --git a/resources/assets/js/store/state.js b/resources/assets/js/store/state.js index f8b69690c..b43e44677 100644 --- a/resources/assets/js/store/state.js +++ b/resources/assets/js/store/state.js @@ -64,6 +64,7 @@ export default { wsListener: {}, wsReadTimeout: null, wsReadWaitData: {}, + wsCallReconnect: 0, // 会员信息 userInfo: {},