WebSocketServer.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.ydtech.modules.admin.controller.websocket;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.ydtech.config.SpringContextUtils;
  6. import com.ydtech.exception.SystemException;
  7. import com.ydtech.modules.admin.dao.SysDeptMapper;
  8. import com.ydtech.modules.admin.model.SysDept;
  9. import com.ydtech.modules.admin.model.SysGeographyPosition;
  10. import com.ydtech.modules.admin.model.SysGeographyPositionRecord;
  11. import com.ydtech.modules.admin.model.SysUser;
  12. import com.ydtech.modules.admin.service.SysDeptService;
  13. import com.ydtech.modules.admin.service.SysGeographyPositionRecordService;
  14. import com.ydtech.modules.admin.service.SysGeographyPositionService;
  15. import com.ydtech.modules.admin.service.SysUserService;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.stereotype.Component;
  18. import javax.websocket.*;
  19. import javax.websocket.server.PathParam;
  20. import javax.websocket.server.ServerEndpoint;
  21. import java.util.Date;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.concurrent.atomic.AtomicInteger;
  25. @Slf4j
  26. @ServerEndpoint(value = "/websocket/{userId}")
  27. @Component
  28. public class WebSocketServer {
  29. private String userId;
  30. private Session session;
  31. private static Map<String, Session> sessionClientMap = new ConcurrentHashMap<>();
  32. /**静态变量,用来记录当前在线连接数*/
  33. private static final AtomicInteger onlineCount = new AtomicInteger(0);
  34. /**
  35. * 连接成功调用的方法
  36. * @param userId
  37. * @param session
  38. */
  39. @OnOpen
  40. public void onOpen(@PathParam("userId") String userId, Session session) {
  41. this.session=session;
  42. this.userId=userId;
  43. if (sessionClientMap.containsKey(userId)) {
  44. sessionClientMap.remove(userId);
  45. // 加入map中
  46. sessionClientMap.put(userId, session);
  47. } else {
  48. // 加入map中
  49. sessionClientMap.put(userId, session);
  50. // 在线数加1
  51. onlineCount.incrementAndGet();
  52. }
  53. editStatus(userId);
  54. log.info("APP用户连接成功:" + userId + ",当前在线人数为:" + onlineCount);
  55. sendMessage("APP用户连接成功");
  56. }
  57. @OnMessage
  58. public void onMessage(String message, Session session) {
  59. JSONObject jsonObject = JSONObject.parseObject(message);
  60. String userId = jsonObject.getString("userId");
  61. String longitude = jsonObject.getString("longitude");
  62. String latitude = jsonObject.getString("latitude");
  63. String province = jsonObject.getString("province");
  64. String city = jsonObject.getString("city");
  65. String district = jsonObject.getString("district");
  66. String street = jsonObject.getString("street");
  67. String streetNum = jsonObject.getString("streetNum");
  68. String poiName = jsonObject.getString("poiName");
  69. String cityCode = jsonObject.getString("cityCode");
  70. saveLocationData(userId, latitude, longitude,province,city,district,street,streetNum,poiName,cityCode);
  71. log.info("服务端收到APP用户消息:{} ,主体:{}", message, userId);
  72. sendMessage("服务端已收到APP用户信息:" +message);
  73. }
  74. @OnClose
  75. public void onClose(@PathParam("userId") String userId, Session session) {
  76. delStatus(userId);
  77. sessionClientMap.remove(userId);
  78. log.info("APP用户断联成功!");
  79. }
  80. @OnError
  81. public void onError(Throwable throwable) {
  82. log.info("websocket异常,错误信息:{}", throwable.getMessage());
  83. }
  84. private void delStatus(String userId) {
  85. SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
  86. SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId);
  87. if (sysGeographyPosition != null) {
  88. sysGeographyPosition.setStatus(0);
  89. sysGeographyPositionService.updateById(sysGeographyPosition);
  90. }
  91. }
  92. private void editStatus(String userId) {
  93. SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
  94. SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId);
  95. if (sysGeographyPosition != null) {
  96. sysGeographyPosition.setStatus(1);
  97. sysGeographyPositionService.updateById(sysGeographyPosition);
  98. }
  99. }
  100. public void sendMessage(String message) {
  101. this.session.getAsyncRemote().sendText(message);
  102. }
  103. public void saveLocationData(String userId, String latitude, String longitude,
  104. String province,String city,String district,
  105. String street,String streetNum,String poiName,String cityCode) {
  106. SysGeographyPositionRecordService sysGeographyPositionRecordService = SpringContextUtils.getBean(SysGeographyPositionRecordService.class);
  107. SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
  108. SysGeographyPosition sysGeographyPosition1 = sysGeographyPositionService.getById(userId);
  109. SysDeptMapper sysDeptMapper = SpringContextUtils.getBean(SysDeptMapper.class);
  110. SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class);
  111. SysUser sysUser = sysUserService.getById(userId);
  112. SysDept sysDept = sysDeptMapper.selectOne(new LambdaQueryWrapper<SysDept>()
  113. .eq(SysDept::getId, sysUser.getDeptId()).eq(SysDept::getDelFlag, 0));
  114. if (sysGeographyPosition1 == null) {
  115. //不存在新增
  116. SysGeographyPosition sysGeographyPosition = new SysGeographyPosition();
  117. sysGeographyPosition.setId(userId);
  118. sysGeographyPosition.setUserName(sysUser.getName());
  119. sysGeographyPosition.setLatitude(latitude);
  120. sysGeographyPosition.setLongitude(longitude);
  121. sysGeographyPosition.setProvince(province);
  122. sysGeographyPosition.setCity(city);
  123. sysGeographyPosition.setDistrict(district);
  124. sysGeographyPosition.setStreet(street);
  125. sysGeographyPosition.setStreetNum(streetNum);
  126. sysGeographyPosition.setPoiName(poiName);
  127. sysGeographyPosition.setCityCode(cityCode);
  128. sysGeographyPosition.setAddress(province + city + district + street+streetNum+poiName+cityCode);
  129. sysGeographyPosition.setCreateTime(new Date());
  130. sysGeographyPosition.setCreateBy(sysUser.getName());
  131. sysGeographyPosition.setSource(sysDept.getManagementSource());
  132. sysGeographyPosition.setLeaderName(sysUser.getLeaderName());
  133. sysGeographyPositionService.save(sysGeographyPosition);
  134. SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition, SysGeographyPositionRecord.class);
  135. sysGeographyPositionRecord.setId(null);
  136. sysGeographyPositionRecord.setUserId(sysGeographyPosition.getId());
  137. sysGeographyPositionRecordService.save(sysGeographyPositionRecord);
  138. }else {
  139. //存在更新
  140. sysGeographyPosition1.setLongitude(longitude);
  141. sysGeographyPosition1.setLatitude(latitude);
  142. sysGeographyPosition1.setProvince(province);
  143. sysGeographyPosition1.setCity(city);
  144. sysGeographyPosition1.setDistrict(district);
  145. sysGeographyPosition1.setStreet(street);
  146. sysGeographyPosition1.setStreetNum(streetNum);
  147. sysGeographyPosition1.setPoiName(poiName);
  148. sysGeographyPosition1.setCityCode(cityCode);
  149. sysGeographyPosition1.setAddress(province + city + district + street+streetNum+poiName+cityCode);
  150. sysGeographyPosition1.setCreateTime(new Date());
  151. sysGeographyPosition1.setSource(sysDept.getManagementSource());
  152. sysGeographyPosition1.setLeaderName(sysUser.getLeaderName());
  153. sysGeographyPositionService.updateById(sysGeographyPosition1);
  154. SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition1, SysGeographyPositionRecord.class);
  155. sysGeographyPositionRecord.setId(null);
  156. sysGeographyPositionRecord.setUserId(sysGeographyPosition1.getId());
  157. sysGeographyPositionRecordService.save(sysGeographyPositionRecord);
  158. }
  159. }
  160. }