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

忻府网站建设网站做哪些比较有意思

忻府网站建设,网站做哪些比较有意思,青岛网站定制开发,做视频网站的挣钱吗SpringBoot实现即时通讯 功能简述 好友管理群组管理聊天模式#xff1a;私聊、群聊消息类型#xff1a;系统消息、文本、语音、图片、视频会话列表、发送消息、接收消息 核心代码 package com.qiangesoft.im.core;import com.alibaba.fastjson2.JSONObject; import com.q…SpringBoot实现即时通讯 功能简述 好友管理群组管理聊天模式私聊、群聊消息类型系统消息、文本、语音、图片、视频会话列表、发送消息、接收消息 核心代码 package com.qiangesoft.im.core;import com.alibaba.fastjson2.JSONObject; import com.qiangesoft.im.constant.ChatTypeEnum; import com.qiangesoft.im.constant.ImMessageBodyTypeEnum; import com.qiangesoft.im.service.IImGroupUserService; import com.qiangesoft.im.util.SpringUtil; import com.qiangesoft.im.pojo.dto.PingDTO; import com.qiangesoft.im.pojo.vo.PongVO; import com.qiangesoft.im.pojo.vo.ImMessageVO; import com.qiangesoft.im.pojo.dto.ImMessageDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component;import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;/*** 聊天会话** author qiangesoft* date 2023-08-30*/ Slf4j ServerEndpoint(/ws/im/{userId}) Component public class ImWebSocketServer {/*** concurrent包的线程安全Set用来存放每个客户端对应的WebSocket对象。*/private static final ConcurrentHashMapLong, ImWebSocketServer WEBSOCKET_MAP new ConcurrentHashMap();/*** 与某个客户端的连接会话需要通过它来给客户端发送数据*/private Session session;/*** 连接建立成功调用的方法用map存客户端对应的WebSocket对象*/OnOpenpublic void onOpen(Session session, PathParam(userId) Long userId) {this.session session;if (WEBSOCKET_MAP.containsKey(userId)) {WEBSOCKET_MAP.remove(userId);WEBSOCKET_MAP.put(userId, this);} else {WEBSOCKET_MAP.put(userId, this);}log.info(User [{}] connection opened, userId);PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.SUCCESS.getCode());pongVO.setContent(连接成功);pongVO.setTimestamp(System.currentTimeMillis());doSendMessage(JSONObject.toJSONString(pongVO));}/*** 收到客户端消息后调用的方法*/OnMessagepublic void onMessage(Session session, PathParam(userId) Long userId, String message) {log.info(User [{}] send a message, content is [{}], userId, message);PingDTO pingDTO null;try {pingDTO JSONObject.parseObject(message, PingDTO.class);} catch (Exception e) {log.error(消息解析失败);e.printStackTrace();}if (pingDTO null || !ImMessageBodyTypeEnum.PING.getCode().equals(pingDTO.getType())) {sendInValidMessage();return;}PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.PONG.getCode());pongVO.setContent(已收到消息~);pongVO.setTimestamp(System.currentTimeMillis());doSendMessage(JSONObject.toJSONString(pongVO));}/*** 连接关闭调用的方法*/OnClosepublic void onClose(Session session, PathParam(userId) Long userId) {close(session, userId);log.info(User {} connection is closed, userId);}/*** 报错** param session* param error*/OnErrorpublic void onError(Session session, Throwable error) {error.printStackTrace();}/*** 指定的userId服务端向客户端发送消息*/public static void sendMessage(ImMessageDTO message) {String chatType message.getChatType();if (ChatTypeEnum.GROUP.getCode().equals(chatType)) {sendGroupMessage(message);}if (ChatTypeEnum.PERSON.getCode().equals(chatType)) {sendPersonMessage(message);}}/*** 指定的userId服务端向客户端发送消息*/public static void offline(Long userId) {ImWebSocketServer webSocketServer WEBSOCKET_MAP.get(userId);if (webSocketServer ! null) {PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.OFFLINE.getCode());pongVO.setContent(设备被挤下线);pongVO.setTimestamp(System.currentTimeMillis());webSocketServer.doSendMessage(JSONObject.toJSONString(pongVO));close(webSocketServer.session, userId);}}/*** 自定义关闭** param session* param userId*/public static void close(Session session, Long userId) {if (WEBSOCKET_MAP.containsKey(userId)) {try {session.close();} catch (IOException e) {e.printStackTrace();}WEBSOCKET_MAP.remove(userId);}}/*** 获取在线用户信息** return*/public static MapLong, ImWebSocketServer getOnlineUser() {return WEBSOCKET_MAP;}/*** 发送无效消息*/private void sendInValidMessage() {PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.FAIL.getCode());pongVO.setContent(无效消息);pongVO.setTimestamp(System.currentTimeMillis());doSendMessage(JSONObject.toJSONString(pongVO));}/*** 发送群组消息** param message*/private static void sendGroupMessage(ImMessageDTO message) {Long receiverId message.getReceiverId();IImGroupUserService groupUserService SpringUtil.getBean(IImGroupUserService.class);ListLong userIdList groupUserService.listUserIdByGroupId(receiverId);MessageHandlerService messageHandlerService SpringUtil.getBean(MessageHandlerService.class);ImMessageVO messageVO messageHandlerService.buildVo(message);PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.MESSAGE.getCode());pongVO.setContent(messageVO);pongVO.setTimestamp(System.currentTimeMillis());String messageStr JSONObject.toJSONString(pongVO);for (Long userId : userIdList) {ImWebSocketServer webSocketServer WEBSOCKET_MAP.get(userId);if (webSocketServer ! null) {if (!userId.equals(message.getSenderId())) {webSocketServer.doSendMessage(messageStr);}}}}/*** 发送私聊消息** param message*/private static void sendPersonMessage(ImMessageDTO message) {Long receiverId message.getReceiverId();ImWebSocketServer webSocketServer WEBSOCKET_MAP.get(receiverId);if (webSocketServer ! null) {MessageHandlerService messageHandlerService SpringUtil.getBean(MessageHandlerService.class);ImMessageVO messageVO messageHandlerService.buildVo(message);PongVO pongVO new PongVO();pongVO.setType(ImMessageBodyTypeEnum.MESSAGE.getCode());pongVO.setContent(messageVO);pongVO.setTimestamp(System.currentTimeMillis());webSocketServer.doSendMessage(JSONObject.toJSONString(pongVO));}}/*** 实现服务器推送到对应的客户端*/private void doSendMessage(String message) {try {this.session.getBasicRemote().sendText(message);} catch (IOException e) {e.printStackTrace();}} }
http://www.hkea.cn/news/14303927/

相关文章:

  • 域名分析网站惠州制作网站软件
  • 深圳中瑞建设集团官方网站wordpress轮播全屏
  • 甘肃温室大棚建设网站网站的集约化建设
  • 合肥定制网站建设公司wordpress常用库
  • 做蛋糕网站的优点宣传型企业网站
  • 河东建设局网站seo搜索引擎推广
  • 什么星网站做调查问卷的如何建设cf提卡网站
  • 有打赏功能的网站模板企业快速建站
  • 网站开发遵循的标准或规范58同城淄博网站建设
  • 网站建设管理权限广州番禺区邮编
  • 网站建设综合实训心得体会网站后台管理系统有哪些
  • 做网站安全维护是什么东东开公司需要什么手续和证件
  • 群晖建设网站国外网站 图片
  • 德州网站建设德州软件app开发公司哪家好
  • 分析竞争对手网站dedecms免费网站模板
  • 用户登录优化大师tv版
  • 微信上打开连接的网站怎么做的宜家设计装修怎么收费
  • 嘉兴网站建设定制网站福州销售网站设计企业
  • seo网站关键词优化快速官网我想自己做一个网站
  • 做淘宝的货源网站wordpress模板 更换
  • 国外销售网站网络营销推广方法有哪几种
  • 外贸网站建设方法需要建设网站的
  • 用pdf怎么做电子书下载网站成都cim软件公司
  • 高性能网站建设进阶指南pdfdw做网站的导航栏
  • 网站建设一定要买数据盘吗wordpress能用一个数据库
  • 网站备案后有什么好处wordpress好用的编辑器插件
  • 做代练网站能备案项目网络图最早开始时间
  • 关于网站建设新闻天津智能网站建设哪家好
  • 网站建设流程包括网站上的漂浮怎么做
  • 山西省建设厅执业资格注册中心网站财税公司怎么找客源