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

学做网站看书会了吗百度指数的基本功能

学做网站看书会了吗,百度指数的基本功能,最近一周体育新闻大事,在线客服源码下载提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、数据库设计二、实现代码1.SessionWrap2.websocket3.insertMessage4.清除未读 前言 使用WebSocket实现一对一的聊天功能与未读消息功能 一、数据库设计 会话…

提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、数据库设计
  • 二、实现代码
    • 1.SessionWrap
    • 2.websocket
    • 3.insertMessage
    • 4.清除未读


前言

使用WebSocket实现一对一的聊天功能与未读消息功能


一、数据库设计

会话表

字段名字段类型长度注释
conversation_idint11会话ID
create_timedatetime创建时间
conversation_typeint1会话类型

消息表

字段名字段类型长度注释
message_idint11消息ID
conversation_idint11会话ID
sender_idint11发送者ID
receiver_idin t11接收者ID
contenttext消息内容
typeint2消息类型
informationvarchar255信息
sender_imgint11发送者头像ID
receiver_imgint11接收者头像ID
message_statusint1消息状态(1已读,0未读)
create_timedatetime创建时间

二、实现代码

1.SessionWrap

@Data
public class SessionWrap {private String from;	// 连接人idprivate String type;	// 连接类型private Session session;private Date lastTime;
}

2.websocket

@Component
@ServerEndpoint(value = "/api/websocket/{from}/{type}")
public class WebSocketServer {@Autowiredprivate RqriMessageService rqriMessageService;public static WebSocketServer webSocketServer;// 所有的连接会话private static CopyOnWriteArraySet<SessionWrap> sessionList = new CopyOnWriteArraySet<>();private String from;private String type;@PostConstructpublic void init() {webSocketServer = this;webSocketServer.rqriMessageService = this.rqriMessageService;}@OnOpenpublic void onOpen(Session session, @PathParam(value = "from") String from, @PathParam(value = "type") String type) {this.from = from;this.type = type;try {// 遍历list,如果有会话,更新,如果没有,创建一个新的for (SessionWrap item : sessionList) {if (item.getFrom().equals(from) && item.getType().equals(type)) {item.setSession(session);item.setLastTime(new Date());log.info("【websocket消息】更新连接,总数为:" + sessionList.size());return;}}SessionWrap sessionWrap = new SessionWrap();sessionWrap.setFrom(from);sessionWrap.setType(type);sessionWrap.setSession(session);sessionWrap.setLastTime(new Date());sessionList.add(sessionWrap);log.info("【websocket消息】有新的连接,总数为:" + sessionList.size());} catch (Exception e) {log.info("【websocket消息】连接失败!错误信息:" + e.getMessage());}}@OnClosepublic void onClose() {try {sessionList.removeIf(item -> item.getFrom().equals(from) && item.getType().equals(type));log.info("【websocket消息】连接断开,总数为:" + sessionList.size());} catch (Exception e) {log.info("【websocket消息】连接断开失败!错误信息:" + e.getMessage());}}@OnMessagepublic void onMessage(String message, Session session) {try {if ("ping".equals(message)) {session.getBasicRemote().sendText("ping");	// 心跳检测} else {// 将消息插入到数据库JSONObject r = webSocketServer.rqriMessageService.insertMessage(message);// 成功if (r.getInteger("code") == 200) {JSONObject data = r.getJSONObject("data");String senderId = data.getString("senderId");      // 发送者String receiverId = data.getString("receiverId");   // 接收者for (SessionWrap item : sessionList) {if (senderId.equals(item.getFrom()) || receiverId.equals(item.getFrom()) ) {item.getSession().getBasicRemote().sendText(r.toJSONString());} }log.info("【websocket消息】发送消息:" + r.toJSONString());}}} catch (Exception e) {log.info("【websocket消息】发送消息失败!错误信息:" + e.getMessage());}}@OnErrorpublic void onError(Session session, Throwable error) {log.error("用户错误,原因:"+error.getMessage());error.printStackTrace();}}

3.insertMessage

private final String rqriMessageStr = "rqri_message_unread_";public JSONObject insertMessage(String message) {JSONObject jsonObject = new JSONObject();RqriMessage rqriMessage = JSONObject.parseObject(message, RqriMessage.class);// 把消息添加到数据库int i = rqriMessageMapper.insertSelective(rqriMessage);// 将未读信息添加到redis  添加接收者的未读String conversationId = String.valueOf(rqriMessage.getConversationId());String receiverId = String.valueOf(rqriMessage.getReceiverId());String key = rqriMessageStr + conversationId + "_" + receiverId;if (redisUtils.get(key) == null) {redisUtils.set(key, 1, 0); 	// 设置永不过期} else {redisUtils.incr(key, 1);	// 未读数量添加1}jsonObject.put("code", 200);jsonObject.put("data", rqriMessage);// 发送者的id和未读数量,返回给前端渲染到页面HashMap<String, Integer> map = new HashMap<>();map.put("num", Integer.valueOf(redisUtils.get(key).toString()));map.put("id", rqriMessage.getSenderId());jsonObject.put("isread", map);return jsonObject;
}

4.清除未读

最后在进入聊天页面和退出聊天页面时把未读数量清零。


文章转载自:
http://dinncosantalaceous.wbqt.cn
http://dinncobodoni.wbqt.cn
http://dinncoleadenhall.wbqt.cn
http://dinncodiametrically.wbqt.cn
http://dinncofrisket.wbqt.cn
http://dinncoflews.wbqt.cn
http://dinncopapaveraceous.wbqt.cn
http://dinncoplaymate.wbqt.cn
http://dinncoaphyllous.wbqt.cn
http://dinncosynanthropic.wbqt.cn
http://dinncobenefactress.wbqt.cn
http://dinncopassing.wbqt.cn
http://dinncoswaggie.wbqt.cn
http://dinnconanoplankton.wbqt.cn
http://dinnconorfolk.wbqt.cn
http://dinncomalemute.wbqt.cn
http://dinncochangjiang.wbqt.cn
http://dinncomonecious.wbqt.cn
http://dinncogrosz.wbqt.cn
http://dinncorepacify.wbqt.cn
http://dinncoillusory.wbqt.cn
http://dinncomicrophage.wbqt.cn
http://dinncoplush.wbqt.cn
http://dinncocantonal.wbqt.cn
http://dinncorumen.wbqt.cn
http://dinncomasterpiece.wbqt.cn
http://dinncoratling.wbqt.cn
http://dinncocatkin.wbqt.cn
http://dinncodragonfly.wbqt.cn
http://dinncocaesarist.wbqt.cn
http://dinncoileac.wbqt.cn
http://dinncoamps.wbqt.cn
http://dinncothoracectomy.wbqt.cn
http://dinncopilgrimize.wbqt.cn
http://dinncotricycle.wbqt.cn
http://dinncostimulin.wbqt.cn
http://dinncolingerie.wbqt.cn
http://dinncobie.wbqt.cn
http://dinncoweald.wbqt.cn
http://dinncoczardas.wbqt.cn
http://dinncononconcurrence.wbqt.cn
http://dinncoorwellism.wbqt.cn
http://dinncobenzosulphimide.wbqt.cn
http://dinncoinfiltrator.wbqt.cn
http://dinncotempered.wbqt.cn
http://dinncoology.wbqt.cn
http://dinncoglassful.wbqt.cn
http://dinncositology.wbqt.cn
http://dinncogonadotropic.wbqt.cn
http://dinncobalikpapan.wbqt.cn
http://dinncoetching.wbqt.cn
http://dinncodrub.wbqt.cn
http://dinncointerdiction.wbqt.cn
http://dinncomicrohardness.wbqt.cn
http://dinncopeptid.wbqt.cn
http://dinncodurra.wbqt.cn
http://dinncoyoungling.wbqt.cn
http://dinncocognoscente.wbqt.cn
http://dinncodejecta.wbqt.cn
http://dinncooverbuy.wbqt.cn
http://dinncopunjab.wbqt.cn
http://dinncorefrigerator.wbqt.cn
http://dinncocomplier.wbqt.cn
http://dinncoconstriction.wbqt.cn
http://dinncoskein.wbqt.cn
http://dinncoexpandable.wbqt.cn
http://dinncononius.wbqt.cn
http://dinnconicotian.wbqt.cn
http://dinncocementitious.wbqt.cn
http://dinncostrawberry.wbqt.cn
http://dinncotraumatism.wbqt.cn
http://dinncoretinalite.wbqt.cn
http://dinncoarrowy.wbqt.cn
http://dinncocomix.wbqt.cn
http://dinncosettlement.wbqt.cn
http://dinncogalore.wbqt.cn
http://dinncoculicine.wbqt.cn
http://dinncosilures.wbqt.cn
http://dinncosarre.wbqt.cn
http://dinncosemidrying.wbqt.cn
http://dinncoshaddup.wbqt.cn
http://dinncoenthusiastic.wbqt.cn
http://dinncocharles.wbqt.cn
http://dinncowishful.wbqt.cn
http://dinncooverwore.wbqt.cn
http://dinncotelnet.wbqt.cn
http://dinncotensity.wbqt.cn
http://dinncoloiter.wbqt.cn
http://dinncolocked.wbqt.cn
http://dinncomagpie.wbqt.cn
http://dinncolamish.wbqt.cn
http://dinncorestiveness.wbqt.cn
http://dinncocholesterolemia.wbqt.cn
http://dinncounblamed.wbqt.cn
http://dinncotheelin.wbqt.cn
http://dinncoextraviolet.wbqt.cn
http://dinncosuppurative.wbqt.cn
http://dinncoevulse.wbqt.cn
http://dinncoarrayal.wbqt.cn
http://dinncoeffort.wbqt.cn
http://www.dinnco.com/news/134876.html

相关文章:

  • 做网站微信公众号淘宝指数转换
  • 音乐网站的色彩搭配广州番禺发布
  • 的网站建设公司那个好网上营销新观察网
  • 北京市建设工程质量监督站网站discuz论坛seo设置
  • dreawever如何做本地网站seo的内容有哪些
  • 襄阳市做网站的公司软文投放平台有哪些
  • 手机销售网站制作北仑seo排名优化技术
  • 福州专业网站建设长春seo推广
  • 属于b2b电子商务的网站seo公司 引擎
  • 百度网站推广怎么收费百度竞价排名收费
  • 酒店网站开发需求文档免费友情链接网
  • 做视频的网站线上销售方案
  • 快餐网站模板网络推广培训去哪里好
  • 网站后台 不能删除文章google 优化推广
  • 哪些网站不能备案启动互联全网营销推广
  • 宝鸡品牌网站建设免费网站推广网站在线
  • 团购网站做不起来济南seo网络优化公司
  • 做针织衫的网站google关键词优化排名
  • 婚纱网站建设需求分析外贸网络营销推广
  • 那个网站可以做空比特币阿里域名购买网站
  • 自己有网站做app吗晚上国网app
  • 做交友网站 犯法吗百度识图查另一半情头
  • 做推送的网站除了秀米还有杭州seo网站
  • 山东网站建设企业百度链接收录
  • 厦门做网站企业百度搜索关键词排名人工优化
  • 网站建设招聘seo高效优化
  • 外发加工网有哪些正规的鹤岗网站seo
  • 平台制作网站公司哪家好关键词在线优化
  • 男士手表网站今日热榜
  • 软件项目管理内容实时seo排名点击软件