上线/离线 通知

This commit is contained in:
kuaifan 2021-06-06 14:10:58 +08:00
parent c8f88dc3ee
commit dd9dbd7b87
7 changed files with 83 additions and 14 deletions

View File

@ -8,9 +8,11 @@ use App\Models\User;
use App\Models\WebSocket; use App\Models\WebSocket;
use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogMsg;
use App\Module\Base; use App\Module\Base;
use App\Tasks\LineTask;
use App\Tasks\PushTask; use App\Tasks\PushTask;
use Cache; use Cache;
use Carbon\Carbon; use Carbon\Carbon;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface; use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\WebSocket\Frame; use Swoole\WebSocket\Frame;
@ -82,6 +84,8 @@ class WebSocketService implements WebSocketHandlerInterface
'fd' => $fd, 'fd' => $fd,
], ],
])); ]));
// 通知上线
Task::deliver(new LineTask($userid, true));
// 重试发送失败的消息 // 重试发送失败的消息
PushTask::resendTmpMsgForUserid($userid); PushTask::resendTmpMsgForUserid($userid);
} }
@ -148,6 +152,8 @@ class WebSocketService implements WebSocketHandlerInterface
public function onClose(Server $server, $fd, $reactorId) public function onClose(Server $server, $fd, $reactorId)
{ {
$this->deleteUser($fd); $this->deleteUser($fd);
// 通知离线
Task::deliver(new LineTask($this->getUserid($fd), false));
} }
/** ****************************************************************************** */ /** ****************************************************************************** */

52
app/Tasks/LineTask.php Normal file
View File

@ -0,0 +1,52 @@
<?php
namespace App\Tasks;
use App\Models\WebSocket;
@error_reporting(E_ALL & ~E_NOTICE);
/**
* 上线、离线通知
* Class LineTask
* @package App\Tasks
*/
class LineTask extends AbstractTask
{
protected $userid;
protected $online;
/**
* LineTask constructor.
* @param $userid
* @param bool $online
*/
public function __construct($userid, bool $online)
{
$this->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,
],
]
]);
}
});
}
}

View File

@ -21,7 +21,7 @@ class PushTask extends AbstractTask
/** /**
* PushTask constructor. * PushTask constructor.
* @param string|array $params * @param array $params
*/ */
public function __construct($params = []) public function __construct($params = [])
{ {
@ -91,8 +91,9 @@ class PushTask extends AbstractTask
* @param array $lists 消息列表 * @param array $lists 消息列表
* @param string|int $key 延迟推送key依据留空立即推送延迟推送时发给同一人同一种消息类型只发送最新的一条 * @param string|int $key 延迟推送key依据留空立即推送延迟推送时发给同一人同一种消息类型只发送最新的一条
* @param int $delay 延迟推送时间默认1秒$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)) { if (!is_array($lists) || empty($lists)) {
return; return;
@ -160,8 +161,19 @@ class PushTask extends AbstractTask
} }
} }
// 记录发送失败的 // 记录发送失败的
if ($addFail) {
$userFail = array_values(array_unique($userFail)); $userFail = array_values(array_unique($userFail));
$tmp_msg_id == 0 && self::addTmpMsg($userFail, $msg); $tmp_msg_id == 0 && self::addTmpMsg($userFail, $msg);
} }
} }
}
/**
* 推送消息(忽略错误)
* @param array $lists 消息列表
*/
public static function pushIgnoreFail(array $lists)
{
self::push($lists, '', 1, false);
}
} }

View File

@ -19,7 +19,9 @@ class WebSocketDialogMsgTask extends AbstractTask
protected $dialogMsgArray; protected $dialogMsgArray;
/** /**
* NoticeTask constructor. * WebSocketDialogMsgTask constructor.
* @param $userid
* @param array $dialogMsgArray
*/ */
public function __construct($userid, array $dialogMsgArray) public function __construct($userid, array $dialogMsgArray)
{ {
@ -27,9 +29,6 @@ class WebSocketDialogMsgTask extends AbstractTask
$this->dialogMsgArray = $dialogMsgArray; $this->dialogMsgArray = $dialogMsgArray;
} }
/**
* @throws \Throwable
*/
public function start() public function start()
{ {
$userids = is_array($this->userid) ? $this->userid : [$this->userid]; $userids = is_array($this->userid) ? $this->userid : [$this->userid];

View File

@ -1,5 +1,5 @@
<template> <template>
<div v-if="$store.state.projectChatShow" class="project-dialog"> <div class="project-dialog">
<DialogWrapper class="project-dialog-wrapper"> <DialogWrapper class="project-dialog-wrapper">
<div slot="head"> <div slot="head">
<div class="dialog-user"> <div class="dialog-user">
@ -98,15 +98,15 @@ export default {
watch: { watch: {
projectDetail() { projectDetail() {
this.getDialogMsg() this.getMsg()
}, },
projectChatShow() { projectChatShow() {
this.getDialogMsg() this.getMsg()
} }
}, },
methods: { methods: {
getDialogMsg() { getMsg() {
if (this.projectChatShow && this.projectDetail.dialog_id) { if (this.projectChatShow && this.projectDetail.dialog_id) {
this.$store.commit('getDialogMsg', this.projectDetail.dialog_id); this.$store.commit('getDialogMsg', this.projectDetail.dialog_id);
} }

View File

@ -2,7 +2,7 @@
<div class="project"> <div class="project">
<PageTitle>{{ $L('项目面板') }}</PageTitle> <PageTitle>{{ $L('项目面板') }}</PageTitle>
<ProjectList/> <ProjectList/>
<ProjectDialog/> <ProjectDialog v-show="$store.state.projectChatShow"/>
</div> </div>
</template> </template>

View File

@ -368,7 +368,7 @@ export default {
if (type === "dialog") { if (type === "dialog") {
const msgData = msgDetail.data; const msgData = msgDetail.data;
const dialog_id = msgData.dialog_id; 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); let index = state.dialogMsgList.findIndex(({id}) => id === msgData.id);
if (index === -1) { if (index === -1) {
if (state.dialogMsgList.length >= 200) { if (state.dialogMsgList.length >= 200) {