当前位置: 首页 > news >正文

即时通讯 离线消息处理初版

离线消息处理

NotOnlineExecute

package com.example.im.infra.executor.send;import com.example.im.endpoint.WebSocketEndpoint;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.io.IOException;
import java.util.Set;/*** @author PC* 不在线处理方式*/
@Component
public class NotOnlineExecute {private final static Logger logger = LoggerFactory.getLogger(NotOnlineExecute.class);private RedisTemplate<String, String> redisTemplate;@Autowiredpublic void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {this.redisTemplate = redisTemplate;}/*** 用户不在线时保存离线消息** @param notOnlineReceiverSet 不在线人员列表* @param message              消息*/public void notOnlineMessageSave(Set<String> notOnlineReceiverSet, String message) {//离线消息notOnlineReceiverSet.forEach(receiver -> redisTemplate.opsForSet().add("offline_messages:" + receiver, message));}/*** 用户上线时进行发送** @param receiverName 接收人*/public void sendOnline(String receiverName) {int receiverNameHashCode = receiverName.hashCode();Set<String> messageSet = redisTemplate.opsForSet().members("offline_messages:" + receiverName);if (CollectionUtils.isEmpty(messageSet)) {logger.info(receiverName + "no offline messages");return;}messageSet.forEach(message -> {try {WebSocketEndpoint.WEB_SOCKET_ENDPOINT_MAP.get(receiverNameHashCode).getSession().getBasicRemote().sendText(message);} catch (IOException e) {logger.error("An error occurred when the user: {} received an offline message: {}", receiverName, message);}});redisTemplate.delete("offline_messages:" + receiverName);}
}

com.example.im.infra.executor.send.DefaultSendExecutor#sendToUser

向redis添加未送达消息

notOnlineReceiverSet = notOnlineReceiverSet.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(notOnlineReceiverSet)) {//处理未送达消息notOnlineExecute.notOnlineMessageSave(notOnlineReceiverSet, generatorMessage(message));logger.info("not online number is " + notOnlineReceiverSet.size());logger.info("The user : {} is not online", String.join(",", notOnlineReceiverSet));
}

com.example.im.endpoint.WebSocketEndpoint#onOpen

用户登录时触发离线消息发送

WEB_SOCKET_ENDPOINT_MAP.put(hashCode, this);
//触发离线消息发送
notOnlineExecute.sendOnline(userName);

测试

test2向test1发送消息,test1不进行连接

test1连接,接收到离线消息

参考资料

[1].处理离线消息代码


http://www.mrgr.cn/news/56317.html

相关文章:

  • STM32CubeIDE(Eclipse)Post-build steps添加带参.exe实现全流程(2):带参调用.exe的几种方法
  • Flux.using 使用说明书
  • 2024年前端开发者必备的20个神器 - 提升效率的终极指南
  • 一起搭WPF架构之LiveCharts.Wpf的简单了解与安装
  • 机器学习方向在算法优化上有哪些创新点?
  • C++面向对象编程学习
  • 【MYSQL】数据库基本操作----DQL(Data Query Language)---基本查询
  • 前端学习笔记(2.0)
  • Java方法重载
  • 进入Neptoon:第二周游戏指南
  • Molmo模型实战
  • Node Checking - Checkboxes and Radio Buttons 节点检查 - 复选框和单选按钮
  • 重生之“我打数据结构,真的假的?”--1.顺序表(无习题)
  • 常见软件生命周期类型
  • QSpinBox、QDoubleSpinBox
  • ArcGIS002:软件自定义设置
  • 在Debian上安装向日葵
  • 目前机器学习算法优化在实际应用中有哪些成功案例?
  • 程序设计基础I-单元测试4(机测+编程题)
  • SpringBoot02:第一个springboot程序
  • 【K8S系列】Kubernetes Pod节点Pending状态及解决方案详解【已解决】
  • 极氪MIX主打一个“够大、够好玩”,期待值拉满~
  • 医院信息化与智能化系统(5)
  • 网址工具大全
  • 浏览器调起摄像头
  • docker安装mysql