no message

This commit is contained in:
kuaifan 2021-06-08 09:58:23 +08:00
parent abadd83ca2
commit 7c6dd3a45b
5 changed files with 49 additions and 28 deletions

View File

@ -86,8 +86,8 @@ class WebSocketService implements WebSocketHandlerInterface
])); ]));
// 通知上线 // 通知上线
Task::deliver(new LineTask($userid, true)); Task::deliver(new LineTask($userid, true));
// 重试发送失败的消息 // 推送离线时收到的消息
PushTask::resendTmpMsgForUserid($userid); Task::deliver(new PushTask("RETRY::" . $userid));
} }
break; break;

View File

@ -21,7 +21,7 @@ class PushTask extends AbstractTask
/** /**
* PushTask constructor. * PushTask constructor.
* @param array $params * @param array|string $params
*/ */
public function __construct($params = []) public function __construct($params = [])
{ {
@ -31,17 +31,23 @@ class PushTask extends AbstractTask
public function start() public function start()
{ {
if (is_string($this->params)) { if (is_string($this->params)) {
$key = $this->params; // 推送缓存
$params = Cache::pull($key); if (Base::leftExists($this->params, "PUSH::")) {
if (is_array($params) && $params['fd']) { $params = Cache::pull($this->params);
$this->params = [$params]; if (is_array($params) && $params['fd']) {
$this->params = [$params];
}
}
// 根据会员ID推送离线时收到的消息
elseif (Base::leftExists($this->params, "RETRY::")) {
self::sendTmpMsgForUserid(intval(Base::leftDelete($this->params, "RETRY::")));
} }
} }
is_array($this->params) && self::push($this->params); is_array($this->params) && self::push($this->params);
} }
/** /**
* 记录发送失败的消息,等上线后重新发送 * 记录离线消息,等上线后重新发送
* @param array $userFail * @param array $userFail
* @param array $msg * @param array $msg
*/ */
@ -64,11 +70,14 @@ class PushTask extends AbstractTask
} }
/** /**
* 根据会员ID重试发送失败的消息 * 根据会员ID推送离线时收到的消息
* @param $userid * @param $userid
*/ */
public static function resendTmpMsgForUserid($userid) private static function sendTmpMsgForUserid($userid)
{ {
if (empty($userid)) {
return;
}
WebSocketTmpMsg::whereCreateId($userid) WebSocketTmpMsg::whereCreateId($userid)
->whereSend(0) ->whereSend(0)
->where('created_at', '>', Carbon::now()->subMinute()) // 1分钟内添加的数据 ->where('created_at', '>', Carbon::now()->subMinute()) // 1分钟内添加的数据
@ -89,9 +98,10 @@ 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 失败后是否保存到临时表,等上线后继续发送 * @param bool $retryOffline 如果会员不在线,等上线后继续发送
* @param bool $andMyself 同时发送给自己其他设备
*/ */
public static function push(array $lists, $key = '', $delay = 1, $addFail = false) public static function push(array $lists, $key = '', $delay = 1, $retryOffline = true, $andMyself = false)
{ {
if (!is_array($lists) || empty($lists)) { if (!is_array($lists) || empty($lists)) {
return; return;
@ -116,8 +126,8 @@ class PushTask extends AbstractTask
continue; continue;
} }
// 发送对象 // 发送对象
$userFail = []; $offline_user = [];
$array = []; $array = $andMyself ? WebSocket::getMyFd() : [];
if ($fd) { if ($fd) {
if (is_array($fd)) { if (is_array($fd)) {
$array = array_merge($array, $fd); $array = array_merge($array, $fd);
@ -134,7 +144,7 @@ class PushTask extends AbstractTask
if ($row->isNotEmpty()) { if ($row->isNotEmpty()) {
$array = array_merge($array, $row->toArray()); $array = array_merge($array, $row->toArray());
} else { } else {
$userFail[] = $uid; $offline_user[] = $uid;
} }
} }
} }
@ -145,7 +155,7 @@ class PushTask extends AbstractTask
$swoole->push($fid, Base::array2json($msg)); $swoole->push($fid, Base::array2json($msg));
$tmp_msg_id > 0 && WebSocketTmpMsg::whereId($tmp_msg_id)->update(['send' => 1]); $tmp_msg_id > 0 && WebSocketTmpMsg::whereId($tmp_msg_id)->update(['send' => 1]);
} catch (\Exception $e) { } catch (\Exception $e) {
$userFail[] = WebSocket::whereFd($fid)->value('userid');
} }
} else { } else {
$key = "PUSH::" . $fid . ":" . $type . ":" . $key; $key = "PUSH::" . $fid . ":" . $type . ":" . $key;
@ -158,20 +168,29 @@ class PushTask extends AbstractTask
Task::deliver($task); Task::deliver($task);
} }
} }
// 记录发送失败 // 记录不在线
if ($addFail) { if ($retryOffline && $tmp_msg_id == 0) {
$userFail = array_values(array_unique($userFail)); $offline_user = array_values(array_unique($offline_user));
$tmp_msg_id == 0 && self::addTmpMsg($userFail, $msg); self::addTmpMsg($offline_user, $msg);
} }
} }
} }
/** /**
* 推送消息(出错后保存临时表,上线后尝试重新发送 * 推送消息(仅推送当前在线的
* @param array $lists 消息列表 * @param array $lists 消息列表
*/ */
public static function pushR(array $lists) public static function pushO(array $lists, $key = '', $delay = 1)
{ {
self::push($lists, '', 1, true); self::push($lists, $key, $delay, false);
}
/**
* 推送消息(同时发送给自己其他设备)
* @param array $lists 消息列表
*/
public static function pushM(array $lists, $key = '', $delay = 1)
{
self::push($lists, $key, $delay, false, true);
} }
} }

View File

@ -9,7 +9,7 @@ use App\Models\WebSocketDialogMsgRead;
/** /**
* 消息通知任务 * 推送回话消息
* Class WebSocketDialogMsgTask * Class WebSocketDialogMsgTask
* @package App\Tasks * @package App\Tasks
*/ */
@ -60,7 +60,7 @@ class WebSocketDialogMsgTask extends AbstractTask
} }
// 开始推送消息 // 开始推送消息
if ($pushIds) { if ($pushIds) {
PushTask::push([ PushTask::pushM([
'userid' => $pushIds, 'userid' => $pushIds,
'msg' => [ 'msg' => [
'type' => 'dialog', 'type' => 'dialog',

View File

@ -110,7 +110,7 @@ export default {
text: this.msgText, text: this.msgText,
}, },
}); });
this.goBottom(); this.autoBottom = true;
// //
$A.apiAjax({ $A.apiAjax({
url: 'dialog/msg/sendtext', url: 'dialog/msg/sendtext',
@ -217,12 +217,12 @@ export default {
goBottom() { goBottom() {
if (this.autoBottom) { if (this.autoBottom) {
this.msgNew = 0;
this.$refs.scroller.autoToBottom(); this.$refs.scroller.autoToBottom();
} }
}, },
goNewBottom() { goNewBottom() {
this.msgNew = 0;
this.autoBottom = true; this.autoBottom = true;
this.goBottom(); this.goBottom();
}, },

View File

@ -114,7 +114,9 @@ export default {
return; return;
} }
if (state.method.isJson(state.cacheProject[project_id])) { if (state.method.isJson(state.cacheProject[project_id])) {
state.projectDetail = state.cacheProject[project_id]; setTimeout(() => {
state.projectDetail = state.cacheProject[project_id];
});
} }
state.projectDetail.id = project_id; state.projectDetail.id = project_id;
// //