diff --git a/app/Services/WebSocketService.php b/app/Services/WebSocketService.php index 8abcd4fd..abc3c2f0 100644 --- a/app/Services/WebSocketService.php +++ b/app/Services/WebSocketService.php @@ -8,9 +8,11 @@ use App\Models\User; use App\Models\WebSocket; use App\Models\WebSocketDialogMsg; use App\Module\Base; +use App\Tasks\LineTask; use App\Tasks\PushTask; use Cache; use Carbon\Carbon; +use Hhxsv5\LaravelS\Swoole\Task\Task; use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface; use Swoole\Http\Request; use Swoole\WebSocket\Frame; @@ -82,6 +84,8 @@ class WebSocketService implements WebSocketHandlerInterface 'fd' => $fd, ], ])); + // 通知上线 + Task::deliver(new LineTask($userid, true)); // 重试发送失败的消息 PushTask::resendTmpMsgForUserid($userid); } @@ -148,6 +152,8 @@ class WebSocketService implements WebSocketHandlerInterface public function onClose(Server $server, $fd, $reactorId) { $this->deleteUser($fd); + // 通知离线 + Task::deliver(new LineTask($this->getUserid($fd), false)); } /** ****************************************************************************** */ diff --git a/app/Tasks/LineTask.php b/app/Tasks/LineTask.php new file mode 100644 index 00000000..9e132109 --- /dev/null +++ b/app/Tasks/LineTask.php @@ -0,0 +1,52 @@ +userid = $userid; + $this->online = $online; + } + + public function start() + { + WebSocket::where('userid', '!=', $this->userid)->chunk(100, function ($list) { + $fd = []; + foreach ($list as $ws) { + $fd[] = $ws->fd; + } + if ($fd) { + PushTask::pushIgnoreFail([ + 'fd' => $fd, + 'msg' => [ + 'type' => 'line', + 'data' => [ + 'userid' => $this->userid, + 'online' => $this->online, + ], + ] + ]); + } + }); + } +} diff --git a/app/Tasks/PushTask.php b/app/Tasks/PushTask.php index 2830b4ae..b2034f2b 100644 --- a/app/Tasks/PushTask.php +++ b/app/Tasks/PushTask.php @@ -21,7 +21,7 @@ class PushTask extends AbstractTask /** * PushTask constructor. - * @param string|array $params + * @param array $params */ public function __construct($params = []) { @@ -91,8 +91,9 @@ class PushTask extends AbstractTask * @param array $lists 消息列表 * @param string|int $key 延迟推送key依据,留空立即推送(延迟推送时发给同一人同一种消息类型只发送最新的一条) * @param int $delay 延迟推送时间,默认:1秒($key填写时有效) + * @param bool $addFail 失败后是否保存到临时表,等上线后继续发送 */ - public static function push(array $lists, $key = '', $delay = 1) + public static function push(array $lists, $key = '', $delay = 1, $addFail = true) { if (!is_array($lists) || empty($lists)) { return; @@ -160,8 +161,19 @@ class PushTask extends AbstractTask } } // 记录发送失败的 - $userFail = array_values(array_unique($userFail)); - $tmp_msg_id == 0 && self::addTmpMsg($userFail, $msg); + if ($addFail) { + $userFail = array_values(array_unique($userFail)); + $tmp_msg_id == 0 && self::addTmpMsg($userFail, $msg); + } } } + + /** + * 推送消息(忽略错误) + * @param array $lists 消息列表 + */ + public static function pushIgnoreFail(array $lists) + { + self::push($lists, '', 1, false); + } } diff --git a/app/Tasks/WebSocketDialogMsgTask.php b/app/Tasks/WebSocketDialogMsgTask.php index 0ebdd64e..1483bc72 100644 --- a/app/Tasks/WebSocketDialogMsgTask.php +++ b/app/Tasks/WebSocketDialogMsgTask.php @@ -19,7 +19,9 @@ class WebSocketDialogMsgTask extends AbstractTask protected $dialogMsgArray; /** - * NoticeTask constructor. + * WebSocketDialogMsgTask constructor. + * @param $userid + * @param array $dialogMsgArray */ public function __construct($userid, array $dialogMsgArray) { @@ -27,9 +29,6 @@ class WebSocketDialogMsgTask extends AbstractTask $this->dialogMsgArray = $dialogMsgArray; } - /** - * @throws \Throwable - */ public function start() { $userids = is_array($this->userid) ? $this->userid : [$this->userid]; diff --git a/resources/assets/js/pages/manage/components/ProjectDialog.vue b/resources/assets/js/pages/manage/components/ProjectDialog.vue index a951bfda..10f216b3 100644 --- a/resources/assets/js/pages/manage/components/ProjectDialog.vue +++ b/resources/assets/js/pages/manage/components/ProjectDialog.vue @@ -1,5 +1,5 @@ diff --git a/resources/assets/js/store/mutations.js b/resources/assets/js/store/mutations.js index e17965f6..9a370349 100644 --- a/resources/assets/js/store/mutations.js +++ b/resources/assets/js/store/mutations.js @@ -368,7 +368,7 @@ export default { if (type === "dialog") { const msgData = msgDetail.data; const dialog_id = msgData.dialog_id; - if (dialog_id == state.dialogId) { + if (dialog_id === state.dialogId) { let index = state.dialogMsgList.findIndex(({id}) => id === msgData.id); if (index === -1) { if (state.dialogMsgList.length >= 200) {