首页/下载资源/后端/微信小程序+springboot及时聊天(通讯)代码

ZIP微信小程序+springboot及时聊天(通讯)代码

m0_5979987823.4KB需要积分:1

资源文件列表:

chatcode.zip 大约有40个文件
  1. chatcode/
  2. chatcode/springboot/
  3. chatcode/springboot/chat/
  4. chatcode/springboot/chat/config/
  5. chatcode/springboot/chat/config/WebSocketConfig.java 1.15KB
  6. chatcode/springboot/chat/controller/
  7. chatcode/springboot/chat/controller/ChatController.java 2.22KB
  8. chatcode/springboot/chat/entry/
  9. chatcode/springboot/chat/entry/ChatMessage.java 242B
  10. chatcode/springboot/chat/mini/
  11. chatcode/springboot/chat/mini/config/
  12. chatcode/springboot/chat/mini/config/MiniWebSocketConfig.java 440B
  13. chatcode/springboot/chat/mini/controller/
  14. chatcode/springboot/chat/mini/controller/MiniWebSocketController.java 2.81KB
  15. chatcode/springboot/chat/mini/mapper/
  16. chatcode/springboot/chat/mini/mapper/SessionPool.java 5.79KB
  17. chatcode/springboot/chat/mini/mapper/WebSocketEndPoint.java 2.84KB
  18. chatcode/springboot/constans/
  19. chatcode/springboot/constans/Constans.java 312B
  20. chatcode/springboot/redis/
  21. chatcode/springboot/redis/config/
  22. chatcode/springboot/redis/config/RedisConfig.java 1.75KB
  23. chatcode/springboot/redis/mapper/
  24. chatcode/springboot/redis/mapper/RedisUtils.java 3.62KB
  25. chatcode/springboot/redis/tool/
  26. chatcode/springboot/redis/tool/SufferVariable.java 584B
  27. chatcode/springboot/tool/
  28. chatcode/springboot/tool/Result.java 1.04KB
  29. chatcode/wx_mini_code/
  30. chatcode/wx_mini_code/chat/
  31. chatcode/wx_mini_code/chat/chat.js 10.19KB
  32. chatcode/wx_mini_code/chat/chat.json 31B
  33. chatcode/wx_mini_code/chat/chat.wxml 4.83KB
  34. chatcode/wx_mini_code/chat/chat.wxss 7.41KB
  35. chatcode/wx_mini_code/rpromise/
  36. chatcode/wx_mini_code/rpromise/request.js 703B
  37. chatcode/wx_mini_code/rpromise/uploadfile.js 787B
  38. chatcode/wx_mini_code/tool/
  39. chatcode/wx_mini_code/tool/form/
  40. chatcode/wx_mini_code/tool/form/form.js 5.4KB

资源介绍:

1.微信小程序ws通讯 2.后端使用springboot的websocket
package com.example.mengchuangyuan.common.chat.mini.mapper; import com.alibaba.fastjson.JSON; import com.example.mengchuangyuan.common.redis.mapper.RedisUtils; import com.example.mengchuangyuan.common.redis.tool.SufferVariable; import com.example.mengchuangyuan.common.tool.DateYMDms; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import javax.websocket.Session; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @Component @Slf4j public class SessionPool { @Autowired private RedisUtils redisUtils; //key-value : userId - 会话(系统创建) public static Map sessions = new ConcurrentHashMap<>();//避免多线程问题 public void close(String sessionId) { //sessionId是在session中添加了一个标识,准确定位某条session for (String userId : SessionPool.sessions.keySet()) { Session session = SessionPool.sessions.get(userId); if (session.getId().equals(sessionId)) { sessions.remove(userId); break; } } } public void sendMessage(String userId, String message) { sessions.get(userId).getAsyncRemote().sendText(message); } //消息的群发,业务逻辑的群发 public void sendMessage(String message) { // redisUtils.cacheValue("chatMessage","String.valueOf(SufferVariable.messageMap)"); for (String sessionId : SessionPool.sessions.keySet()) { SessionPool.sessions.get(sessionId).getAsyncRemote().sendText(message); } } //点对点的消息推送 public void sendMessage(Map params) { log.info("消息内容:"+String.valueOf(params)); long mid = System.currentTimeMillis(); Map formUser = (Map) params.get("formUser"); Map toUser = (Map) params.get("toUser"); String userId = formUser.get("openid").toString(); String toUserId = toUser.get("openid").toString(); // String msg = params.get("message").toString(); String type = params.get("type").toString(); String linkType = params.get("linkType").toString(); //获取用户session Session session = sessions.get(toUserId); Map keyMap; List setOpenid; Map map; List list; String uid = userId+toUserId; params.put("mid",mid); if (SufferVariable.messageMap.get(userId+toUserId)==null&&SufferVariable.messageMap.get(toUserId+userId)==null){ // messageMap.put(userId+toUserId,userId+toUserId); map = new HashMap<>(); list = new ArrayList<>(); map.put(linkType,list); SufferVariable.messageMap.put(uid,map); // list.add(params); }else { if (SufferVariable.messageMap.get(userId+toUserId)!=null){ uid = userId+toUserId; }else { uid = toUserId+userId; } map = (Map) SufferVariable.messageMap.get(uid); list = (List) map.get(linkType); } //获取历史记录 if (type.equalsIgnoreCase("history")){ if (sessions.get(userId) != null) { Map map2 = new HashMap<>(); // map = (Map) SufferVariable.messageMap.get(uid); map2.put("type", "isHistory"); map2.put("message",list); sessions.get(userId).getAsyncRemote().sendText(JSON.toJSONString(map2)); } return; } String nowDate = DateYMDms.getUtilDate(); if (list.size()!=0) { Map lastMap = (Map) list.get(list.size() - 1); String date = DateYMDms.getYMDms(5, lastMap.get("nowDate").toString()); params.put("date", date); }else { params.put("date", nowDate); } // if (SufferVariable.openidKeys.get(linkType)==null){ // keyMap = new HashMap<>(); // }else { // keyMap = (Map) SufferVariable.openidKeys.get(linkType); // } // if (keyMap.get(userId)==null){ // setOpenid = new ArrayList<>(); // }else { // log.info(String.valueOf(SufferVariable.openidKeys)); // setOpenid = (List) keyMap.get(userId); // } if (SufferVariable.openidKeys.get(linkType) == null){ setOpenid = new ArrayList<>(); }else { setOpenid = (List) SufferVariable.openidKeys.get(linkType); } setOpenid.add(uid); Set set = new LinkedHashSet<>(setOpenid); setOpenid.clear(); setOpenid.addAll(set); // keyMap.put(userId,setOpenid); SufferVariable.openidKeys.put(linkType,setOpenid); params.put("nowDate",nowDate); list.add(params); map.put(linkType,list); SufferVariable.messageMap.put(uid,map); redisUtils.cacheValue("chatMessage",SufferVariable.messageMap); redisUtils.cacheValue("chatOpenidKeys",SufferVariable.openidKeys); if (session != null) { log.info(uid+":"+String.valueOf(SufferVariable.messageMap.get(uid))); session.getAsyncRemote().sendText(JSON.toJSONString(params)); } // log.info(String.valueOf(messageMap)); // params.remove("formUserId"); //session不为空的情况下进行点对点推送 } }
100+评论
captcha